test with PID control to screen on your computer
Dependencies: Motor_with_encoder MODSERIAL QEI mbed
Fork of Project_motor by
main.cpp
- Committer:
- vera1
- Date:
- 2017-10-06
- Revision:
- 2:80177e90c1e6
- Parent:
- 1:e0f6cdefcd6e
- Child:
- 3:4520d0ca4e56
File content as of revision 2:80177e90c1e6:
#include "mbed.h" #include "MODSERIAL.h" #include "encoder.h" // this program can turn the motor clockwise or counterclockwise depending on the value p or n typed into the terminal. with 's' you can stop the motor MODSERIAL pc(USBTX,USBRX); PwmOut motorspeed(D5); DigitalOut motorposition(D4); DigitalOut led1(D8); DigitalOut led2(D11); AnalogIn pot(A1); DigitalIn button1(D13); DigitalIn button2(D12); Ticker potmeterreadout; Ticker encoderreadout; Encoder motor1(PTD0,PTC4); // motor 1 is from encoder, otherwise it would be a pain in the ass to change all that code to motorencoder float PwmPeriod = 1.0/5000.0; /* void readoutencoder() { pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); } */ volatile float potvalue = 0.0; volatile float position = 0.0; void readpot(){ potvalue = pot.read(); //position = motor1.getPosition(); pc.printf("pos: %d, speed %f reference velocity = %.2f\r\n",motor1.getPosition(), motor1.getSpeed(), potvalue); led1 = potvalue; motorspeed = potvalue; } void writevalues(){ } int main() { pc.baud(9600); potmeterreadout.attach(readpot,0.2f); motorspeed.period(0.0001f); //float motorspeed = 0.0f; while (true) { //pc.printf("reference velocity = %.2f\r\n", potvalue); if ((button2 == 1)&&(button1 == 0)) { motorposition = 0; // motor turns anticlockwise led2 = 0; } if ((button2 ==0)&&(button1 ==1)){ motorposition = 1; // motor turns anticlockwise led2 = 1; } //} } }