Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed HC_SR04_Ultrasonic_Library
Revision 1:60ad9c191ea9, committed 2019-03-14
- Comitter:
- QingshuZhang
- Date:
- Thu Mar 14 14:37:20 2019 +0000
- Parent:
- 0:aed6099bd419
- Commit message:
- TDPS3;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L298.cpp Thu Mar 14 14:37:20 2019 +0000 @@ -0,0 +1,50 @@ +#include "L298_H.h" +#include <mbed.h> + + +L298::L298(PinName pin1, PinName pin2, PinName pin3):m_motorEnable(pin1),m_motorBw(pin2),m_motorFw(pin3){ + /*class constructor : initializes the motors with + forward enable and speed of 0.3 */ + + //m_dir = true; + //m_speed = 0.2; + + m_motorEnable.period_ms(1); + + SetDirection(1); + SetSpeed(0.2); +} + + +void L298::SetDirection(bool dir){ + /*set direction of one of the sides depending on pwmSelect + direction : 1 go forward ,0 go backwards*/ + + + + if(dir){ + m_motorBw = 0; + m_motorFw = 1; + } + else{ + m_motorBw = 1; + m_motorFw = 0; + } + + + m_prevDir = dir; + +} + + +void L298::SetSpeed(float speed){ + /*set speed on the pwm which is point by m_pwmPtr (set by SetDirection) + speed : the speed given to the motor, ranges from 0 to 1*/ + + if(speed<=0) m_motorEnable.write(0); + else if(speed>=1.0)m_motorEnable.write(1.0); + else m_motorEnable.write(speed); + +} + +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/L298_H.h Thu Mar 14 14:37:20 2019 +0000 @@ -0,0 +1,37 @@ +#ifndef L298_H +#define L298_H + +#include <mbed.h> + +//RobotControl is used to control the direction and speed of the robot +//attributes : the pinouts of the enable and pwm forward and backwards +//methods : SetSpeed and SetDirection to set speed and set direction + +class L298 +{ +private: + + DigitalOut m_motorBw; + DigitalOut m_motorFw; + PwmOut m_motorEnable; + + + bool m_prevDir; + + +public: + + bool m_dir; + float m_speed; + + L298(PinName pin1, PinName pin2, PinName pin3); //initializes the pins, direction and speed + + void SetDirection(bool dir); //sets direction of robot, forward or backwards + void SetSpeed(float speed); //sets speed of robot regardless of direction + +}; + + + + +#endif \ No newline at end of file
--- a/RobotControl.cpp Thu Mar 14 09:28:30 2019 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,52 +0,0 @@ -#include "RobotControl_H.h" -#include <mbed.h> - - -RobotControl::RobotControl(PinName pin1, PinName pin2, PinName pin3):m_motorEnable(pin1),m_motorBw(pin2),m_motorFw(pin3){ - /*class constructor : initializes the motors with - forward enable and speed of 0.3 */ - - //m_dir = true; - //m_speed = 0.2; - - m_motorBw.period_ms(1); - m_motorFw.period_ms(1); - - SetDirection(1); - SetSpeed(0.2); -} - - -void RobotControl::SetDirection(bool dir){ - /*set direction of one of the sides depending on pwmSelect - direction : 1 go backwards ,0 go forward*/ - - m_motorBw.write(0); - m_motorFw.write(0); - - wait(0.001); - - if(!dir){ - m_motorEnable = false; - m_pwmPtr = &m_motorFw; - }else{ - m_motorEnable = true; - m_pwmPtr = &m_motorBw; - } - - m_prevDir = dir; - -} - - -void RobotControl::SetSpeed(float speed){ - /*set speed on the pwm which is point by m_pwmPtr (set by SetDirection) - speed : the speed given to the motor, ranges from 0 to 1*/ - - if(speed<=0) m_pwmPtr->write(0); - else if(speed>=1.0)m_pwmPtr->write(1.0); - else m_pwmPtr->write(speed); - -} - -
--- a/main.cpp Thu Mar 14 09:28:30 2019 +0000 +++ b/main.cpp Thu Mar 14 14:37:20 2019 +0000 @@ -1,14 +1,14 @@ -#include <RobotControl_H.h> +#include <L298_H.h> #include <mbed.h> -Timer t; + Timer t1; //Timer used to record the time duration -Timer t2; + DigitalOut trigpin(PTD4); DigitalIn echopin(PTE20); -DigitalOut trigpin2(); -DigitalIn echopin2(); +DigitalOut trigpin2(PTD1); +DigitalIn echopin2(PTC9); DigitalOut test(PTE30); DigitalOut led1(PTD2); DigitalOut led2(PTD3); //two LED indicates if there's any obstacles nearby @@ -17,24 +17,46 @@ bool dirR; float speed; +int getDis (DigitalIn echopin) { +Timer t; +int distance; +float duration; +while (!echopin); // read the duration time + t.start(); + while (echopin); + t.stop(); + duration = t.read_us(); + distance = (duration/2) / 29.1; + t.reset(); + printf("distance: %d\n",distance); //read the distance + test = 1; + wait_ms(100); + test=0; + return distance; +} + + int main() { -RobotControl controlLeft(PTA12,PTA4,PTA5); -RobotControl controlRight(PTA13,PTD5,PTD0); -dirR=0; -dirL=0; +L298 controlLeft(PTA12,PTA4,PTA5); +L298 controlRight(PTA13,PTD5,PTD0); +dirR=1; +dirL=1; speed=0.2; +int distance; +int distance2; +int totalT; controlLeft.SetDirection(dirL); controlRight.SetDirection(dirR); controlLeft.SetSpeed(speed); controlRight.SetSpeed(speed); +test =1; +led1=1; +led2 =1; while (1) { t1.start(); - int totalT; - float duration; - float duration2; - int distance = 0; - int distance2 = 0; + distance = 0; + distance2 = 0; test = 0; trigpin = 0; // low trig signal for 2us trigpin2 = 0; @@ -44,7 +66,7 @@ wait_us(10); trigpin = 0; // stay low trig level trigpin2 = 0; - while (!echopin); // read the duration time + /* while (!echopin); // read the duration time t.start(); while (echopin); t.stop(); @@ -60,11 +82,10 @@ t.reset (); t2.rest(); printf("distance: %d\n",distance); //read the distance - printf("distance2: %d\n",distance2); //read the distance - test = 1; - wait_ms(100); - test=0; - + printf("distance2: %d\n",distance2); //read the distance */ + + distance = getDis(echopin); + distance2 = getDis(echopin2); if (distance < 20|| distance2 <20) { // This is where the LED On/Off happens @@ -79,7 +100,7 @@ controlRight.SetSpeed(speed); dirR=1; dirL=0; - controlLeft.SetDirection(dirl); + controlLeft.SetDirection(dirL); controlRight.SetDirection(dirR); //obstacle on the right, turn left } else { //obstacle on the left or in the middle, turn right @@ -98,8 +119,8 @@ speed = 0.2; controlLeft.SetSpeed(speed); controlRight.SetSpeed(speed); - dirR=0; - dirL=0; + dirR=1; + dirL=1; controlLeft.SetDirection(dirL); controlRight.SetDirection(dirR); }