DCMotor
DC motors are a fundamental component of FIRST Tech Challenge robots. They are used to provide power and movement to various parts of the robot, such as wheels, arms, and other mechanisms. These motors are direct current and brushed. DC Motors come in various sizes and power ratings built for different applications so be sure to select the perfect one for you.
Motors have two primary running methods, setting their power or target position. The most common method is setting the power the motor receives, controlling the motor to rotational speed and direction. The second is setting the target position of the motor. This involves using an encoder to record the rotational positional of a motor and then rotating it unless it reaches the target position.
Here are their pros and cons:
setPower
Pros:
Direct control: Provides immediate control over the motor's speed and direction.
Real-time adjustments: Allows for dynamic adjustments based on sensor feedback or other conditions.
Cons:
Requires continuous input: The motor needs constant power input to maintain its desired speed or direction.
Less precise: Can be less precise for tasks requiring accurate positioning.
Potential for overshoot: May overshoot the target position if not carefully controlled.
setTargetPosition:
Pros:
Precise positioning: Ensures the motor moves to a specific target position.
Closed-loop control: Uses feedback (e.g., from an encoder) to maintain the target position.
Less prone to overshoot: Reduces the risk of overshooting the target.
Cons:
Slower response time: May take longer to reach the target position compared to setPower.
Requires additional hardware: Often requires encoders or other sensors for accurate position feedback.
More complex programming: Can be more complex to implement due to the closed-loop control.
Generally setPower is more used for active control like moving a drivetrain. setTargetPosition is more suitable for tasks that require precise positioning like aligning a robot arm.
Lets see how each method is typically used:
setPower:
There are 3 steps, setting the power, sleeping the program, and then turning off the power.
Setting the power
What this code does is that it tells the motor what direction to move and how fast to move it. If the power is positive, the motor moves counter-clockwise. If the power is negative, the motor moves counter-clockwise. The motor power ranges from -1 to 1. The greater the magnitude of the motor, the faster it goes! The power doesn’t have to be a whole number either! It can be a decimal like 0.1 or -0.7.
Sleeping the program
By calling this block, the program doesn’t execute for the specified amount of milliseconds. This may seem dumb but it has an important purpose: it gives the motor direction in step 1 time to move the robot. Just setting the power of a motor doesn’t make the robot move! Instead it defines the motor’s behavior. To let the motor move the robot, you need to give it time, literally! Sleeping the program will allow the robot time to move around.
Stopping the robot.
To stop the robot, set the power of the motor to 0.
By completely cutting off the power the motor receives, it will completely stop.
Although the robot will automatically stop moving when it has fully executed its code, setting the power to 0 is a helpful way to ensure the robot moves precisely how it was intended.
Typically steps 1 and 2 are repeated several times to create more complex movements before stopping the robot. Step 3 may be incorporated, however, to allow other parts of the robot, like a claw or slide, to move before resuming locomotion.
setTargetPosition:
Unlike setPower, there are only 2 steps, setting the target position and sleeping the program.
Setting the target position
What this code does is that it tells the motor what position to move to. Based on the encoder’s readings the motor will automatically rotate until it has reached the desired position.
Sleeping the program
By calling this block, the program doesn’t execute for the specified amount of milliseconds. This may seem dumb but it has an important purpose: it gives the motor in step 1 time to move the desired position. Just setting the target position of a motor doesn’t make the robot move! Instead it defines the motor’s behavior. To let the motor move the robot, you need to give it time, literally! Sleeping the program will allow the robot time to move around. When the motor has arrived at the target position, it will automatically stop moving so no further action is necessary.
Now that you have learned both ways of controlling a motor, try to experiment to find the right method for you. Remember to customize the approach based on the motor and its purpose to achieve optimal results.