Programming Servo Basics
Last updated
Last updated
Let's start by reviewing how to access servos within Blocks. At the top of the Categorize Blocks section there is a drop down menu for Actuators. When the menu is selected it will drop down two choices: DcMotor or Servo. Selecting Servo will open a side window filled with various servo related blocks.
The block above will change names depending on the name of the servo in a configuration file. If there are multiple servos in a configuration file the arrow next to test_servo will drop down a menu of all the servos in a configuration.
Different block options will appear when using a continuous rotation servo.
Let's start by programming our servo to rotate to the default 1 position!
From the Servo menu, we will primarily be using the block
Select Save OpMode in the upper lefthand corner in the programming interface.
Let's give our program a try. Take a moment to observe what happens.
When running our program for the first time, we should have seen our servo move itself to position 1 and maintain that position. But what happens if we run it again? Does the servo move?
If your servo did not move as expected, double check your wiring and port are correct compared to your configuration.
Try running this op mode on the test bed and consider the following question:
What is different from the previous run?
In many applications starting the servo in a known state, like at position zero, is beneficial to the operation of a mechanism. Setting the servo to the known state in the initialization ensures it is in the correct position when the OpMode runs.
Take a moment to think about where setting the servo to a known state during initialization may be helpful before moving to the next section!
Add this block to the op mode code within the .
Click on the number block to change from to .
The intent of the is to set the position of the servo. If the servo is already in the set position when a code is run, it will not change positions. Lets try adding another block and see what changes.
In this case, we do not want our servo to reset to 0 every time our code repeats. Because of this where do you think we would snap in our block?
Recall when we discussed the section marked by the comment during Programming Essentials. Since we only want our servo to reset ONCE we will request it do so during the initialization process when the code is first activated, but before play is pressed.
Go ahead and click a block into place to match the code below: