Nicolae Marton / Mbed 2 deprecated TDP3_OOP

Dependencies:   mbed

Committer:
Nicolaemf
Date:
Sat Jan 26 18:18:13 2019 +0000
Revision:
0:a02a278513d9
Child:
3:54c41af9e119
implementation of robot motor driver code, in seperate files and using oop;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nicolaemf 0:a02a278513d9 1 #include "RobotControl_H.h"
Nicolaemf 0:a02a278513d9 2 #include <mbed.h>
Nicolaemf 0:a02a278513d9 3
Nicolaemf 0:a02a278513d9 4
Nicolaemf 0:a02a278513d9 5 RobotControl::RobotControl(PinName pin1, PinName pin2, PinName pin3):m_motorEnable(pin1),m_motorBw(pin2),m_motorFw(pin3){
Nicolaemf 0:a02a278513d9 6 /*class constructor : initializes the motors with
Nicolaemf 0:a02a278513d9 7 forward enable and speed of 0.3 */
Nicolaemf 0:a02a278513d9 8
Nicolaemf 0:a02a278513d9 9 m_dir = true;
Nicolaemf 0:a02a278513d9 10 m_speed = 0.3;
Nicolaemf 0:a02a278513d9 11 SetDirection(m_dir);
Nicolaemf 0:a02a278513d9 12 SetSpeed(m_speed);
Nicolaemf 0:a02a278513d9 13 }
Nicolaemf 0:a02a278513d9 14
Nicolaemf 0:a02a278513d9 15
Nicolaemf 0:a02a278513d9 16 void RobotControl::SetDirection(bool dir){
Nicolaemf 0:a02a278513d9 17 /*set direction of one of the sides depending on pwmSelect
Nicolaemf 0:a02a278513d9 18 direction : 1 go forward ,0 go backwards */
Nicolaemf 0:a02a278513d9 19
Nicolaemf 0:a02a278513d9 20 m_motorBw.write(0);
Nicolaemf 0:a02a278513d9 21 m_motorFw.write(0);
Nicolaemf 0:a02a278513d9 22
Nicolaemf 0:a02a278513d9 23 wait(0.1);
Nicolaemf 0:a02a278513d9 24
Nicolaemf 0:a02a278513d9 25 if(dir){
Nicolaemf 0:a02a278513d9 26 m_motorEnable = true;
Nicolaemf 0:a02a278513d9 27 m_pwmPtr = &m_motorFw;
Nicolaemf 0:a02a278513d9 28 }else{
Nicolaemf 0:a02a278513d9 29 m_motorEnable = false;
Nicolaemf 0:a02a278513d9 30 m_pwmPtr = &m_motorBw;
Nicolaemf 0:a02a278513d9 31 }
Nicolaemf 0:a02a278513d9 32
Nicolaemf 0:a02a278513d9 33 m_prevDir = dir;
Nicolaemf 0:a02a278513d9 34
Nicolaemf 0:a02a278513d9 35 }
Nicolaemf 0:a02a278513d9 36
Nicolaemf 0:a02a278513d9 37
Nicolaemf 0:a02a278513d9 38 void RobotControl::SetSpeed(float speed){
Nicolaemf 0:a02a278513d9 39 /*set speed on the pwm which is point by m_pwmPtr (set by SetDirection)
Nicolaemf 0:a02a278513d9 40 speed : the speed given to the motor, ranges from 0 to 1*/
Nicolaemf 0:a02a278513d9 41
Nicolaemf 0:a02a278513d9 42 if(speed<=1.0 && speed > 0.0){
Nicolaemf 0:a02a278513d9 43 m_pwmPtr->write(speed);
Nicolaemf 0:a02a278513d9 44 }else{
Nicolaemf 0:a02a278513d9 45 m_pwmPtr->write(0);
Nicolaemf 0:a02a278513d9 46 }
Nicolaemf 0:a02a278513d9 47 }
Nicolaemf 0:a02a278513d9 48
Nicolaemf 0:a02a278513d9 49