Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Files at this revision

API Documentation at this revision

Comitter:
kyleliangus
Date:
Fri May 05 01:21:33 2017 +0000
Parent:
7:6f5cb6377bd4
Child:
9:1d8e4da058cd
Commit message:
Created Motor class

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
main.h Show annotated file Show diff for this revision Revisions of this file
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
--- a/main.cpp	Fri May 05 00:08:59 2017 +0000
+++ b/main.cpp	Fri May 05 01:21:33 2017 +0000
@@ -13,10 +13,12 @@
 #define I_CONSTANT 0.0000025
 #define D_CONSTANT 0.25
 
+#include "QEI.h"
+#define PULSES 880
 
 int main()
 {
-    enableMotors();
+    //enableMotors();
     //Set highest bandwidth.
     gyro.setLpBandwidth(LPFBW_42HZ);
     serial.baud(9600);
@@ -33,13 +35,21 @@
     greenLed.write(0);
     blueLed.write(1);
     
-    rightWheelForward(0.1);
-    leftWheelForward(0.1);
+    //rightWheelForward(0.1);
+    //leftWheelForward(0.1);
+
+    // PA_1 is A of right
+    // PA_0 is B of right
+    // PA_5 is A of left
+    // PB_3 is B of left
+    QEI encoder0( PA_5, PB_3, NC, PULSES, QEI::X4_ENCODING );
+    QEI encoder1( PA_1, PA_0, NC, PULSES, QEI::X4_ENCODING );
 
     while (1) {
-
-    wait(0.1);
-        serial.printf("%i, %i, %i\n", gyro.getGyroX(), gyro.getGyroY(), gyro.getGyroZ());
+        
+        wait(0.1);
+        //serial.printf("%i, %i, %i\n", gyro.getGyroX(), gyro.getGyroY(), gyro.getGyroZ());
+        serial.printf("Pulse Count=> e0:%d, e1:%d      \r\n", encoder0.getPulses(),encoder1.getPulses());
 
         //reading = Rec_4.read();
 //        serial.printf("reading: %f\n", reading);
--- a/main.h	Fri May 05 00:08:59 2017 +0000
+++ b/main.h	Fri May 05 01:21:33 2017 +0000
@@ -5,6 +5,7 @@
 #include "ITG3200.h"
 
 // Motors
+/*
 PwmOut left1(PB_7);
 PwmOut left2(PB_8);
 PwmOut right1(PA_10);
@@ -12,6 +13,7 @@
 
 DigitalOut enableLeftMotor(PB_4);
 DigitalOut enableRightMotor(PB_5);
+*/
 
 // RGB LED
 DigitalOut redLed(PC_0);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/motor.cpp	Fri May 05 01:21:33 2017 +0000
@@ -0,0 +1,23 @@
+#include "mbed.h"
+#include "motor.h"
+
+void Motor::backward(double voltage) {
+    forw.write(voltage);
+    back.write(0);
+}
+
+void Motor::forward(double voltage) {
+    forw.write(0);
+    back.write(voltage);
+}
+
+
+void Motor::brake() {
+    forw.write(BRAKE_VOLTAGE);
+    back.write(BRAKE_VOLTAGE);
+}
+
+void Motor::coast() {
+    forw.write(0);
+    back.write(0);
+}
\ No newline at end of file
--- a/motor.h	Fri May 05 00:08:59 2017 +0000
+++ b/motor.h	Fri May 05 01:21:33 2017 +0000
@@ -2,75 +2,46 @@
 #define MOTOR_H
 
 #include "mbed.h"
-#include "main.h"
-
-#include "QEI.h"
+//#include "main.h"
 
 #define BRAKE_VOLTAGE 0.2
 
+/*
 extern PwmOut left1;
 extern PwmOut left2;
 extern PwmOut right1;
 extern PwmOut right2;
 
 extern DigitalOut enableLeftMotor;
-extern DigitalOut enableRightMotor;
+extern DigitalOut enableRightMotor; */
+
+class Motor
+{
+    public:
+        Motor( PinName f, PinName b, PinName e ) : 
+            forw( f ), back( b ), enableMotor( e )
+        {
+            enableMotor.write( 1 );
+        }
+    
+        void backward( double voltage );
+        void forward( double voltage );
+        void brake();
+        void coast();
+        
+    private:
+        PwmOut forw;
+        PwmOut back;
+        DigitalOut enableMotor;  
+};
 
 //QEI leftWheel(
 
+/*
 inline void enableMotors(){
     enableLeftMotor.write(1);
     enableRightMotor.write(1);
-}
-
-inline void rightWheelBackward(double voltage) {
-    right1.write(voltage);
-    right2.write(0);
-}
-
-inline void rightWheelForward(double voltage) {
-    right1.write(0);
-    right2.write(voltage);
-}
-
-inline void leftWheelForward(double voltage) {
-    left1.write(0);
-    left2.write(voltage);
-}
-
-inline void leftWheelBackward(double voltage) {
-    left1.write(voltage);
-    left2.write(0);
-}
+}*/
 
-inline void brakeLeftWheel() {
-    left1.write(BRAKE_VOLTAGE);
-    left2.write(BRAKE_VOLTAGE);
-}
-
-inline void brakeRightWheel() {
-    right1.write(BRAKE_VOLTAGE);
-    right2.write(BRAKE_VOLTAGE);
-}
-
-inline void brake() {
-    brakeLeftWheel();
-    brakeRightWheel();
-}
-
-inline void coastLeftWheel() {
-    left1.write(0);
-    left2.write(0);
-}
-
-inline void coastRightWheel() {
-    right1.write(0);
-    right2.write(0);
-}
-
-inline void coast() {
-    coastLeftWheel();
-    coastRightWheel();
-}
 
 #endif
\ No newline at end of file