Stephen Covrig / Mbed 2 deprecated Motor Control via PyQt

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
covrigs
Date:
Fri Dec 09 19:58:50 2016 +0000
Commit message:
For ECE 111, Lab 4. KL46Z receives serial data from a python GUI and controls a motor via an L293D.

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
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.cpp	Fri Dec 09 19:58:50 2016 +0000
@@ -0,0 +1,30 @@
+#include "mbed.h"
+#include "Motor.h"
+
+Motor::Motor(PinName Positive, PinName Negative): _positive(Positive), _negative(Negative){
+     _positive.period(0.03f);      // .03 second period
+     _positive.write(0.25f);       // initial duty cycle
+     _negative.period(0.03f);      // .03 second period
+    _negative.write(0.25f);        // initial duty cycle
+     
+}   
+
+
+void Motor::Spin(float move) {
+    
+    if(move >= .51){                //direction and speed when percentage is above .51
+     _positive = (move-.5);
+     _negative = 0;
+     }
+     
+    else if(move <= .49){           //direction and speed when percentae is below .49
+     _positive = 0;
+     _negative = (.5-move);
+     }
+     
+    else if(move > .49 && move < .51){      //stop motor when precentage is about .50
+     _positive = 0;
+     _negative = 0;
+     }
+     
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Motor.h	Fri Dec 09 19:58:50 2016 +0000
@@ -0,0 +1,24 @@
+#include "mbed.h"
+ 
+#ifndef MBED_MOTOR_H
+#define MBED_MOTOR_H
+#define LEFT 1
+#define RIGHT 2
+
+
+class Motor {
+ 
+public:
+    
+    Motor (PinName Positive, PinName Negative);
+    
+    
+    void Spin(float move);
+    
+protected:
+     PwmOut  _positive;
+     PwmOut  _negative;
+};
+ 
+#endif
+ 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Dec 09 19:58:50 2016 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+#include "Motor.h"
+
+Serial pc(USBTX,USBRX);     // Declare serial connection
+Motor my_motor(PTB1,PTB0);  // Declare motor and motor pin addresses
+
+int main() {
+    
+    pc.baud(9600);
+    int slidervalue = 64;
+    int readvalue = 0;
+    
+    
+    while (1) {
+        
+
+        readvalue = int(pc.getc());     // reads the value from the serial port and stores it as "readvalue"
+        
+        if (readvalue >=1 && readvalue <=127) {    // checks to make sure "readvalue" is within a valid range
+            slidervalue = readvalue;
+        }
+        
+        my_motor.Spin((float(slidervalue) - 1.0)/126.0);    // turns "slidervalue" into a percentage and sends it to the motor's Spin function
+
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Dec 09 19:58:50 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0
\ No newline at end of file