Qingshu Zhang / Mbed 2 deprecated new_HC-SR504

Dependencies:   mbed HC_SR04_Ultrasonic_Library

Committer:
QingshuZhang
Date:
Thu Mar 14 09:28:30 2019 +0000
Revision:
0:aed6099bd419
TDPS3;

Who changed what in which revision?

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