myWRIO
C++ framework for NationalInstruments myRIO
myRIO::Motor Class Reference

Combination of PWM and Encoder to work with motors. More...

#include <Motor.h>

Public Member Functions

 Motor (uint32_t port, bool defaultDirection, double angularCoef)
 Motors configuration. More...
 
void setSpeed (double speed)
 Set the speed in duty cycle. More...
 
void setAngularSpeed (double speed)
 Set the speed in angular speed. More...
 
void setAngularSpeedAndDirection (double speed)
 Set the speed in angular speed and the direction. More...
 
double getSpeed ()
 Get the speed of the motor. More...
 
void setDirection (bool dir)
 Set the direction of the motor. More...
 
bool getDefaultDirection ()
 Get the default direction of the motor. More...
 
void setInterrupt (std::function< void(long, bool)> func, unsigned int threshold)
 
long getEncoderPulses ()
 Get the Encoder values. More...
 
 ~Motor ()
 Destructor Stop the motor by seting the speed to 0. More...
 

Private Attributes

double speed
 The duty cycle of the motor. More...
 
double angularCoef
 Angular coefficient of the motors The relation between the angular speed and the duty cycle. More...
 
bool defaultDirection
 Default direction of the motors In our case : ClockWise for the left motor and Conter ClockWise for the right motor. More...
 
bool direction
 Direction of the motors. More...
 
PWMchannel
 The pin where the PWM will be generated. More...
 
Encoderenc
 Attached encodeur In our case : Encoder A attached to the PWMA_0 (PWM0) and Encoder B attached to the PWMA_1 (PWM1). More...
 
uint8_t pin
 The pin where the PWM will be generated. More...
 

Detailed Description

Combination of PWM and Encoder to work with motors.

Examples
example_motor_pid_encoder.cpp, project_car.cpp, and project_pendulum.cpp.

Definition at line 22 of file Motor.h.

Constructor & Destructor Documentation

◆ Motor()

Motor::Motor ( uint32_t  port,
bool  defaultDirection,
double  angularCoef 
)

Motors configuration.

Parameters
portthe PWM port (PWM[A:C]_[0:2])
defaultDirectionthe default direction of the motor
angularCoefthe angular coefficient of the motor In our case : code to configurate only two motors. The code can be easly changed to control more motors

Definition at line 12 of file Motor.cpp.

◆ ~Motor()

Motor::~Motor ( )

Destructor Stop the motor by seting the speed to 0.

Stop the thread of the encoders.

Definition at line 103 of file Motor.cpp.

Member Function Documentation

◆ getDefaultDirection()

bool Motor::getDefaultDirection ( )

Get the default direction of the motor.

Returns
the direction; CW -> 1 , CCW -> 0
Examples
example_motor_pid_encoder.cpp.

Definition at line 81 of file Motor.cpp.

◆ getEncoderPulses()

long Motor::getEncoderPulses ( )

Get the Encoder values.

Returns
return the value of the encoder

Definition at line 29 of file Motor.cpp.

◆ getSpeed()

double Motor::getSpeed ( )

Get the speed of the motor.

Returns
the speed of the motor

Definition at line 74 of file Motor.cpp.

◆ setAngularSpeed()

void Motor::setAngularSpeed ( double  speed)

Set the speed in angular speed.

Parameters
speedthe speed (angular speed) of the motor In this function the changment of the speed is done from a value °/s [0:850].
Examples
example_motor_pid_encoder.cpp.

Definition at line 48 of file Motor.cpp.

◆ setAngularSpeedAndDirection()

void Motor::setAngularSpeedAndDirection ( double  speed)

Set the speed in angular speed and the direction.

Parameters
speedthe speed (angular speed) and the direction of the motor In this function the changment of the speed is done from a value °/s [0:850]. The direction is done with the the funtion setDirection
Examples
example_motor_pid_encoder.cpp.

Definition at line 60 of file Motor.cpp.

◆ setDirection()

void Motor::setDirection ( bool  dir)

Set the direction of the motor.

Parameters
dirthe direction; CW -> 1 , CCW -> 0

Definition at line 87 of file Motor.cpp.

◆ setInterrupt()

void Motor::setInterrupt ( std::function< void(long, bool)>  func,
unsigned int  threshold 
)
Examples
example_motor_pid_encoder.cpp.

Definition at line 96 of file Motor.cpp.

◆ setSpeed()

void Motor::setSpeed ( double  speed)

Set the speed in duty cycle.

Parameters
speedthe speed (duty cycle) of the motor In this function the changment of the speed is done from a value in percentage [0:100].

Definition at line 39 of file Motor.cpp.

Member Data Documentation

◆ angularCoef

double myRIO::Motor::angularCoef
private

Angular coefficient of the motors The relation between the angular speed and the duty cycle.

For exemple for our motor for the 100% duty cycle we had an angular speed of 884,6 °/s. Which means that our coeffient is angular speed * 100% = 8,846.

Definition at line 36 of file Motor.h.

◆ channel

PWM* myRIO::Motor::channel
private

The pin where the PWM will be generated.

Definition at line 45 of file Motor.h.

◆ defaultDirection

bool myRIO::Motor::defaultDirection
private

Default direction of the motors In our case : ClockWise for the left motor and Conter ClockWise for the right motor.

Definition at line 41 of file Motor.h.

◆ direction

bool myRIO::Motor::direction
private

Direction of the motors.

Definition at line 44 of file Motor.h.

◆ enc

Encoder* myRIO::Motor::enc
private

Attached encodeur In our case : Encoder A attached to the PWMA_0 (PWM0) and Encoder B attached to the PWMA_1 (PWM1).

Definition at line 46 of file Motor.h.

◆ pin

uint8_t myRIO::Motor::pin
private

The pin where the PWM will be generated.

Definition at line 49 of file Motor.h.

◆ speed

double myRIO::Motor::speed
private

The duty cycle of the motor.

Definition at line 35 of file Motor.h.


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