Motor

Dependents:   balance_all

Files at this revision

API Documentation at this revision

Comitter:
ckalintra
Date:
Wed May 16 10:28:22 2018 +0000
Commit message:
Motor

Changed in this revision

motor.cpp Show annotated file Show diff for this revision Revisions of this file
motor.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 8a7754ecb574 motor.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor.cpp	Wed May 16 10:28:22 2018 +0000
@@ -0,0 +1,79 @@
+#include "motor.h"
+#include "mbed.h"
+#define control_speed 1
+
+
+void MOTOR::stop() {//stop the motors
+     dir1 = 0;
+     dir2 = 0;
+     dir3 = 0;
+     dir4 = 0;
+     pwm1.write(0);
+     pwm2.write(0);
+}
+
+void MOTOR::balance(float speed) {//for balancing the robot
+    stop();//stop the motor
+    if (speed<0)//if speed is - go backward  
+    {
+     dir1 = 1;
+     dir2 = 0;
+     dir3 = 0;
+     dir4 = 1;
+     float temp = abs(speed);//use PID output to detrmine the speed
+     pwm1.write(temp);
+     pwm2.write(temp);
+     }
+    else if (speed>0)//if speed is + go forward
+    {
+     dir1 = 0;
+     dir2 = 1;
+     dir3 = 1;
+     dir4 = 0;
+     float temp = abs(speed);
+     pwm1.write(temp);
+     pwm2.write(temp);
+     } 
+}
+
+void MOTOR::forward() {//go forward 
+     stop(); 
+     dir1 = 0;
+     dir2 = 1;
+     dir3 = 1;
+     dir4 = 0;
+     pwm1.write(control_speed);
+     pwm2.write(control_speed);
+}
+     
+void MOTOR::backward() {//go backward  
+     stop(); 
+     dir1 = 1;
+     dir2 = 0;
+     dir3 = 0;
+     dir4 = 1;
+     pwm1.write(control_speed);
+     pwm2.write(control_speed);
+}
+
+void MOTOR::left() {//turn left    
+     dir1 = 1;
+     dir2 = 0;
+     dir3 = 1;
+     dir4 = 0;
+     pwm1.write(control_speed);
+     pwm2.write(control_speed);
+}
+
+void MOTOR::right() {//turn right   
+     dir1 = 0;
+     dir2 = 1;
+     dir3 = 0;
+     dir4 = 1;
+     pwm1.write(control_speed);
+     pwm2.write(control_speed);
+}
+
+
+    
+
diff -r 000000000000 -r 8a7754ecb574 motor.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor.h	Wed May 16 10:28:22 2018 +0000
@@ -0,0 +1,22 @@
+#ifndef MOTOR_H
+#define MOTOR_H
+
+#include "mbed.h"
+
+extern DigitalOut dir1, dir2, dir3, dir4;
+extern PwmOut pwm1, pwm2;
+extern Serial pc;
+class MOTOR {
+public:
+    void balance(float speed);//counter the falling of the robot
+    void forward();//go forward
+    void backward();//go backward
+    void left();//turn left
+    void right();//turn right
+    void stop();//stop the motors
+protected:
+    
+
+};
+
+#endif