Can Opener - ME 507
MotorDriver Class Reference

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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MotorDriver()

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.

Parameters
new_idThe ID of the motor, which has no effect on the function of the MotorDriver object
posThe pin name, i.e. PA0 or PC5, of the pin that is connected to the positive terminal of the motor.
negThe pin name of the pin that is connected to the negative terminal of the motor
enableThe pin name of the pin that is connected to the enable pin on the physical motor driver chip
PWM_boundThe PWM value, from 0 to 100, that is the maximum PWM value that can be written to either the positive or negative pin
b_delayThe delay in ms, that the motor will break before switching the rotation direction of the motor - delay is blocking

Member Function Documentation

◆ brake()

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

◆ getDirection()

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.

Returns
The direction of the motor, as specified above

◆ getID()

uint8_t MotorDriver::getID ( void  )

Returns the ID set in the constructor.

Returns
The ID of the MotorDriver object

◆ getPWM()

float MotorDriver::getPWM ( void  )

Returns the PWM value that the motor is currently being driven at.

Returns
The current PWM value being sent to the motor

◆ move()

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%.

Parameters
new_PWMThe PWM that the motor will be driven at, from -1*PWMbound <= new_PWM <= PWM_bound

The documentation for this class was generated from the following files: