EEEN21000
GROUP 30 Line Following Buggy
Loading...
Searching...
No Matches
Motor Class Reference

Motor class to control the motors. More...

#include <motor.h>

Public Member Functions

 Motor (PinName left, PinName right, PinName direction_left, PinName direction_right, PinName enable)
 Constructor. More...
 
void setEnable (int enable)
 Set the enable pin of the motor driver board, given the enable. More...
 
void setEnable ()
 Toggle the enable pin of the motor driver board, if no argument is given. More...
 
int getEnable ()
 Get the enable status of the motor driver board. More...
 
void setFrequency (float frequency)
 Set the frequency of the PwmOut. More...
 
float getFrequency () const
 Get the frequency of the PwmOut. More...
 
void setDirection (char c, int direction)
 Set the direction of the motors, provided the direction. More...
 
void setDirection (char c)
 Toggle the direction of the motors, if no direction given. More...
 
std::pair< int, int > getDirection () const
 Get the direction of the motors. More...
 
void setDutycycle (char c, float dutycycle)
 Set the duty cycle of the motors. More...
 
std::pair< float, float > getDutycycle ()
 Get the duty cycle of the motors. More...
 

Static Public Member Functions

Cruising Methods

All cruising methods to control the motors include forward, reverse, turnleft and turnright. All methods apply the following sequence:

  • Disable the enable pin.
  • Set the direction of the motors.
  • Set the duty cycle of the motors.
  • Reset the counter of the encoders.
  • Toggle enable pin.
  • When Encoder::getDistance() > distance : Disable enable pin.
static void forward (const double distance, Motor *const motor, Encoder *const left_encoder, Encoder *const right_encoder)
 Control the motor to cruise forward. More...
 
static void reverse (const double distance, Motor *const motor, Encoder *const left_encoder, Encoder *const right_encoder)
 Control the motor to cruise backwards. More...
 
static void turnleft (const double angle, Motor *const motor, Encoder *const left_encoder, Encoder *const right_encoder)
 Control the motor to turn left. More...
 
static void turnright (const double angle, Motor *const motor, Encoder *const left_encoder, Encoder *const right_encoder)
 Control the motor to turn right. More...
 

Public Attributes

PwmOut left_motor
 Left motor PwmOut. More...
 
PwmOut right_motor
 Right motor PwmOut. More...
 

Private Attributes

DigitalOut m_direction_left
 Direction for the left motor. More...
 
DigitalOut m_direction_right
 Direction for the right motor. More...
 
DigitalOut m_enable
 Enable pin to motor driver board. More...
 
float m_period
 Period of PWM output. More...
 

Detailed Description

Motor class to control the motors.

This class includes configuration method of left and right motors. It also features cruising methods to move forward, backwards, turn left and turn left.

Constructor & Destructor Documentation

◆ Motor()

Motor::Motor ( PinName  left,
PinName  right,
PinName  direction_left,
PinName  direction_right,
PinName  enable 
)

Constructor.

Construct a motor object. Using unipolar mode (0), pins are sunk to ground instead of using DigitalOut.write(0).

Parameters
leftLeft motor pin.
rightRight motor pin.
direction_leftDirection left motor.
direction_rightDirection right motor.
enableEnable pin.

Member Function Documentation

◆ setEnable() [1/2]

void Motor::setEnable ( int  enable)

Set the enable pin of the motor driver board, given the enable.

Parameters
enable
  • 1 Enabled
  • 0 Disabled

◆ setEnable() [2/2]

void Motor::setEnable ( )

Toggle the enable pin of the motor driver board, if no argument is given.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

◆ getEnable()

int Motor::getEnable ( )

Get the enable status of the motor driver board.

Returns
int Enable status.
  • 1 Enabled.
  • 0 Disabled.

◆ setFrequency()

void Motor::setFrequency ( float  frequency)

Set the frequency of the PwmOut.

Parameters
frequencyFrequency of the PWM.

◆ getFrequency()

float Motor::getFrequency ( ) const

Get the frequency of the PwmOut.

Returns
float Frequency of the PWM.

◆ setDirection() [1/2]

void Motor::setDirection ( char  c,
int  direction 
)

Set the direction of the motors, provided the direction.

Parameters
cWhich motor direction to change.
direction
  • 1 forward.
  • 0 reverse.

◆ setDirection() [2/2]

void Motor::setDirection ( char  c)

Toggle the direction of the motors, if no direction given.

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

Parameters
cWhich motor direction to change.

◆ getDirection()

std::pair< int, int > Motor::getDirection ( ) const

Get the direction of the motors.

Returns
std::pair<int,int> { m_direction_left, m_direction_right }.

◆ setDutycycle()

void Motor::setDutycycle ( char  c,
float  dutycycle 
)

Set the duty cycle of the motors.

Parameters
cWhich motor to set duty cycle.
  • 'L' Left motor.
  • 'R' Right motor.
  • otherwise both motors.
dutycycleDuty cycle in percentange within [0.0,1.0]. If duty cycle is out of range, it saturates at 0 and 1.

◆ getDutycycle()

std::pair< float, float > Motor::getDutycycle ( )

Get the duty cycle of the motors.

Returns
std::pair<float,float> {Left motor duty cycle, Right motor duty cycle}.

◆ forward()

static void Motor::forward ( const double  distance,
Motor *const  motor,
Encoder *const  left_encoder,
Encoder *const  right_encoder 
)
static

Control the motor to cruise forward.

Parameters
distanceDistance to travelled before stopping.
motorMotor object.
left_encoderLeft Encoder object.
right_encoderRight Encoder object.

◆ reverse()

static void Motor::reverse ( const double  distance,
Motor *const  motor,
Encoder *const  left_encoder,
Encoder *const  right_encoder 
)
static

Control the motor to cruise backwards.

Parameters
distanceDistance to travelled before stopping.
motorMotor object.
left_encoderLeft Encoder object.
right_encoderRight Encoder object.

◆ turnleft()

static void Motor::turnleft ( const double  angle,
Motor *const  motor,
Encoder *const  left_encoder,
Encoder *const  right_encoder 
)
static

Control the motor to turn left.

Parameters
angleAngle to make before stopping.
motorMotor object.
left_encoderLeft Encoder object.
right_encoderRight Encoder object.

◆ turnright()

static void Motor::turnright ( const double  angle,
Motor *const  motor,
Encoder *const  left_encoder,
Encoder *const  right_encoder 
)
static

Control the motor to turn right.

Parameters
angleAngle to make before stopping.
motorMotor object.
left_encoderLeft Encoder object.
right_encoderRight Encoder object.

Member Data Documentation

◆ m_direction_left

DigitalOut Motor::m_direction_left
private

Direction for the left motor.

◆ m_direction_right

DigitalOut Motor::m_direction_right
private

Direction for the right motor.

◆ m_enable

DigitalOut Motor::m_enable
private

Enable pin to motor driver board.

◆ m_period

float Motor::m_period
private

Period of PWM output.

\({m\_period} = \frac{1}{frequency}\\\)

◆ left_motor

PwmOut Motor::left_motor

Left motor PwmOut.

◆ right_motor

PwmOut Motor::right_motor

Right motor PwmOut.


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