![]() |
Can Opener - ME 507
|
Class which controls a servo motor. More...
#include <MotorDriver.h>
Public Member Functions | |
| MotorDriver (uint8_t new_id, uint8_t pos, uint8_t neg, uint8_t enable, uint8_t PWM_bound, uint16_t b_delay=0) | |
| Constructor for the MotorDriver class. More... | |
| void | move (float new_PWM) |
| Sets a new PWM value for the motor to be driven at. More... | |
| void | brake (void) |
| Puts the motor into braking mode. More... | |
| float | getPWM (void) |
| Returns the PWM value that the motor is currently being driven at. More... | |
| uint8_t | getID (void) |
| Returns the ID set in the constructor. More... | |
| int8_t | getDirection (void) |
| Returns the current direction the motor is spinning. More... | |
Protected Attributes | |
| uint8_t | id |
| The id of the MotorDriver object. | |
| float | PWM |
| The current PWM value. | |
| int8_t | PWM_max |
| The maximum PWM allowed. | |
| int8_t | PWM_min |
| The minimum PWM allowed. | |
| uint8_t | pinA |
| The positive pin connected to the motor. | |
| uint8_t | pinB |
| The negative pin connected to the motor. | |
| uint8_t | pinEN |
| The enable pin. | |
| uint16_t | brake_delay |
| The time that the motor will brake when switching directions. | |
| int8_t | direction |
| The current direction of the motor: 1 = forward, 0 = stopped, -1 = reverse. | |
Class which controls a servo motor.
This class allows for PWM control of a servo motor. The motor must be a simple 2-wire servo motor. The class includes protections against quick reversal of the motor with a (blocking) brake delay. The class will also allow for a maximum PWM to be used.
| MotorDriver::MotorDriver | ( | uint8_t | new_id, |
| uint8_t | pos, | ||
| uint8_t | neg, | ||
| uint8_t | enable, | ||
| uint8_t | PWM_bound, | ||
| uint16_t | b_delay = 0 |
||
| ) |
Constructor for the MotorDriver class.
This constructor sets various parameters for the MotorDriver object. These include the pins the motor is connected to, the enable pin, the max/min PWM that can be sent to the motors, the braking delay, and an ID. Note that the braking delay at this point is blocking, so only short delays should be used, if at all. Another note - if the PWM bound is set to a number above 100, the bound will be set to 100.
| new_id | The ID of the motor, which has no effect on the function of the MotorDriver object |
| pos | The pin name, i.e. PA0 or PC5, of the pin that is connected to the positive terminal of the motor. |
| neg | The pin name of the pin that is connected to the negative terminal of the motor |
| enable | The pin name of the pin that is connected to the enable pin on the physical motor driver chip |
| PWM_bound | The PWM value, from 0 to 100, that is the maximum PWM value that can be written to either the positive or negative pin |
| b_delay | The delay in ms, that the motor will break before switching the rotation direction of the motor - delay is blocking |
| void MotorDriver::brake | ( | void | ) |
Puts the motor into braking mode.
When this function is called, PWM_bound is sent to both the positive and negative poles of the motor. This is braking mode for many motor drivers, instead of coast mode, as MotorDriver::move(0) would cause
| int8_t MotorDriver::getDirection | ( | void | ) |
Returns the current direction the motor is spinning.
If the motor is spinning forward, 1 is returned. If spinning backwards, -1 is returned. If the motor is stopped, 0 is returned.
| uint8_t MotorDriver::getID | ( | void | ) |
Returns the ID set in the constructor.
| float MotorDriver::getPWM | ( | void | ) |
Returns the PWM value that the motor is currently being driven at.
| void MotorDriver::move | ( | float | new_PWM | ) |
Sets a new PWM value for the motor to be driven at.
This PWM value is allowed to be any number from -1*PWM_bound (from the constructor) to PWM_bound. If a PWM value is outside the bounds, the motor will be driven at the appropriate bound. A positive PWM is moving forward, negative moving backward. Note that these PWM values are the percentage. Calling MotorDriver::move(1) will rotate forward at 1% PWM, not 100%.
| new_PWM | The PWM that the motor will be driven at, from -1*PWMbound <= new_PWM <= PWM_bound |