# 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.&#x20;

\\

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcT1puQveYO0MvSYk1pnmLm9_lOdmSbTZ0hcMSvRkuqUKU0ILGqkkPzgCf8mWfSO7Iue2DCZTva0dgKOqTQnixsmc3dwKDp44aa8IZyzFO_mIUhi-5uHEhCVBfsXjS8LRPpZRcIXGOFmgwlY8XBBI9cugg?key=lO4s9Q5_O1IGJj2hlVtDxQ)

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.&#x20;

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.

1. Setting the power

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcSSlISJ3ia8UU7YVWAwCZlmThc3dC9d8h04ygbJKAIcnHFBJglxZ1kvoz5yt-zJ_ZUroDe6MHwMpOeOWkzTotqfMgO1tixHDpClav-I4UV8ruoEOCVF30HDTCcaTZpTwm3SSsxugLQTjaI9e6MqfiH6XET?key=lO4s9Q5_O1IGJj2hlVtDxQ)

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.

2. Sleeping the program

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcxuET-wO0D5zx6Oo5bYLIl0Eu3bHN8VZ5ORn186yBhcJiK_XYal5IBp8cngXG-NVuL0P3PsaOX4k__3NUiHfsGQ-98Cyl46H_nP8u7h8nUErDkA86-YJCamgYU7aBvon_uDBNCIs5w7y4q81w6HunMFi4l?key=lO4s9Q5_O1IGJj2hlVtDxQ)

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.

3. Stopping the robot.

To stop the robot, set the power of the motor to 0.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcNU8961Y7cfPVtNLoYKIRD_U1i9Y5pRCInLk9Q93nhMCaZrWquKzdvqSGxa4p0uXwoATog9Cy_C2dO0qYWle0O5IcoqYVQlM0_i8nPkkudirYV7YOVqp-LBUpJ1xZmtW88JeVi3nx_MRNh6CVXVisACBp6?key=lO4s9Q5_O1IGJj2hlVtDxQ)

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.&#x20;

1. Setting the target position

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXervjUlvEbS6sf1o3TgCcM6JNPGQAL3brQnK_YqelFHyGegRbOtvtpq6gXM6CKrwi4zsorG6dw3yXadBiGDJkUX1AgJfggvEyvzDY2bpSYVQXNGHB6WmXMH0k6Jk2Dw0wXRtSqK4VF6kJWWLKuANkbffag?key=lO4s9Q5_O1IGJj2hlVtDxQ)

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.

2. Sleeping the program

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXcxuET-wO0D5zx6Oo5bYLIl0Eu3bHN8VZ5ORn186yBhcJiK_XYal5IBp8cngXG-NVuL0P3PsaOX4k__3NUiHfsGQ-98Cyl46H_nP8u7h8nUErDkA86-YJCamgYU7aBvon_uDBNCIs5w7y4q81w6HunMFi4l?key=lO4s9Q5_O1IGJj2hlVtDxQ)

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.

\\
