Stephen Covrig
/
Motor Control via PyQt
KL46Z code to use in conjunction with a PyQt GUI over a serial communication.
Here is the Python code for the PyQt Gui...
from PyQt4.QtGui import * from PyQt4.QtCore import * import sys import serial def printNumber(number): print number ser = serial.Serial('COM5',9600) #state which port the KL46Z is connected to and the baud rate ser.write(unichr(number).encode()) #encode number from slider and send the encoded character to KL46Z if __name__=="__main__": app=QApplication(sys.argv) slider=QSlider(Qt.Horizontal) slider.setRange(1,127) slider.setFixedWidth(400) slider.setSliderPosition(64) #create a QApplication and QSlider QObject.connect(slider,SIGNAL("valueChanged(int)"),printNumber) #call the printNumber funciton each time the value on the slider changes slider.show() #start the loop sys.exit(app.exec_())
Revision 0:f2c6afc8e527, committed 2016-12-09
- 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
diff -r 000000000000 -r f2c6afc8e527 Motor.cpp --- /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
diff -r 000000000000 -r f2c6afc8e527 Motor.h --- /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
diff -r 000000000000 -r f2c6afc8e527 main.cpp --- /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
diff -r 000000000000 -r f2c6afc8e527 mbed.bld --- /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