We're doing the motor encoders first because I'd like to remind you that the motor encoder is also a sensor. We have used the sensor before, but this time, we will look at it in a little more detail. If you look at the top of the motor there is an arrow that indicates the direction of positive rotation. This is what we will use to determine whether the motor turns forward or reverse. The encoder sends information back to the brain and the brain uses that information to control the motor speed and direction. The amount that the motor rotates can also be controlled very precisely.
There are essentially four types of commands for the motor. The sensing commands all start with a prefix get these commands will all get information from the motor income Then there is the reset motor encoder command to set the motor encoder to zero. The set motor commands all have the prefix set, and they will start the motor spinning or set the motor spinning. The stop motor commands have the prefix stop. And of course, they are used to stop the motors. We should be familiar with this because we've done this in the robot movement lesson.
When we were doing those lessons, you may have noticed in some of the earlier examples that the arm falls back to the start position as soon as the command ends. Now let me show you a way how to fix this. To make the commands we need available we need to set the menu level which can be found in the window tab to expert or super user. Now we have some new commands available They are move motor target set motor break mode, set motor reversed, set motor target. Let's look at the set motor break mode first. This is part of a program we used earlier to stop the arm when encoder reached 360 degrees.
It worked fine on my robot, but if there was something heavy in the claw, the arm didn't keep its position when I took my finger off the touch button we'll use the set motor break mode command to fix this issue. In this command we need to specify the motor followed by the break mode. There are three break modes we can use coast, which lets the motor rotate freely after a stop command and break which slows the rotation down faster when the motor is given the stop command and the last one Hold is the one we are looking for hold will make the motor hold its current position. Even if we try and force it to move we add the state of motor break mode command before we start and stop the motor. This will set the mode for the specified motor for the rest of the program.
If we wanted different break mode later in the program, we'll have to write another set break mode command to change that. This program will now keep the arm at the position it stopped, even if we press down on the arm. The other two new commands we have are just different ways to tell the motor to move to a specific location. Set motor target tells the motor to go to a certain position regardless of what its current position is. In this example, we want the motor to go to the hundred 90 degree position, no matter where the motor currently is 4560 or even 270 degrees, it will go to 180 degrees. If it is at 270 it will reverse to 180.
The move motor target command is different because it will move the motor for the specified degrees. So from whatever point we start, the motor will move for the target amount of degrees. Don't confuse these two commands. It's a very easy mistake to make