Simon Krogedal / Karbot_wheel_control

Dependencies:   mbed ros_lib_melodic

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers motor.cpp Source File

motor.cpp

00001 /* Karbot motor class
00002  * Written by Simon Krogedal
00003  * 27/05/21
00004  * Team 9 4th Year project
00005  * 
00006  * for NUCLEO-F401RE
00007  * 
00008  */
00009  
00010 
00011  #include "motor.h"
00012  
00013 motor::motor(PinName pwm_pin, PinName dir_pin, double period) : output(pwm_pin), dir(dir_pin), T(period) {
00014     output.period(T);                   //this period is not going to change during the run
00015     dir = 1;                            //forward is active high
00016     dutCyc = 0.1;                       //just need a default value to avoid bugs
00017     stop();                             //starts off
00018 }
00019 
00020 void motor::drive(void) {
00021     driving = 1;
00022     output.write(dutCyc);
00023 }
00024 
00025 void motor::stop(void) {
00026     driving = 0;
00027     output.write(0.0);                              //just constant low
00028 }
00029 
00030 void motor::setOut(double dc) {                            //set duty cycle as a number between -1 and 1, where 1 is full force forward, -1 is backwards and 0 is still
00031     if(dc < 0.0) {
00032         dir = 0;
00033         if(dc < -1.0)
00034             dutCyc = 1.0;
00035         else
00036             dutCyc = -dc;
00037     }
00038     else {
00039         dir = 1;
00040         if(dc > 1.0)
00041             dutCyc = 1.0;
00042         else
00043             dutCyc = dc;
00044     }
00045     if(driving)
00046         output.write(dutCyc);
00047 }
00048 
00049 double motor::getPeriod(void) {return T;}
00050 
00051 double motor::getDuty(void) {return dutCyc;}
00052