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_())
main.cpp
- Committer:
- covrigs
- Date:
- 2016-12-09
- Revision:
- 0:f2c6afc8e527
File content as of revision 0:f2c6afc8e527:
#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 } }