test with PID control to screen on your computer
Dependencies: Motor_with_encoder MODSERIAL QEI mbed
Fork of Project_motor by
main.cpp@0:bc949f735b8d, 2017-10-03 (annotated)
- Committer:
- vera1
- Date:
- Tue Oct 03 15:07:02 2017 +0000
- Revision:
- 0:bc949f735b8d
- Child:
- 1:e0f6cdefcd6e
motor speed control with pot meter, direction control with buttons. Led in progress.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vera1 | 0:bc949f735b8d | 1 | #include "mbed.h" |
vera1 | 0:bc949f735b8d | 2 | #include "MODSERIAL.h" |
vera1 | 0:bc949f735b8d | 3 | #include "encoder.h" |
vera1 | 0:bc949f735b8d | 4 | // 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 |
vera1 | 0:bc949f735b8d | 5 | |
vera1 | 0:bc949f735b8d | 6 | MODSERIAL pc(USBTX,USBRX); |
vera1 | 0:bc949f735b8d | 7 | PwmOut motorspeed(D5); |
vera1 | 0:bc949f735b8d | 8 | DigitalOut motorposition(D4); |
vera1 | 0:bc949f735b8d | 9 | DigitalOut led1(D8); |
vera1 | 0:bc949f735b8d | 10 | DigitalOut led2(D7); |
vera1 | 0:bc949f735b8d | 11 | AnalogIn pot(A1); |
vera1 | 0:bc949f735b8d | 12 | DigitalIn button1(D9); |
vera1 | 0:bc949f735b8d | 13 | DigitalIn button2(D10); |
vera1 | 0:bc949f735b8d | 14 | Ticker potmeterreadout; |
vera1 | 0:bc949f735b8d | 15 | //Ticker encoderreadout; |
vera1 | 0:bc949f735b8d | 16 | //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 |
vera1 | 0:bc949f735b8d | 17 | |
vera1 | 0:bc949f735b8d | 18 | |
vera1 | 0:bc949f735b8d | 19 | float PwmPeriod = 1.0/5000.0; |
vera1 | 0:bc949f735b8d | 20 | /* |
vera1 | 0:bc949f735b8d | 21 | void readoutencoder() |
vera1 | 0:bc949f735b8d | 22 | { |
vera1 | 0:bc949f735b8d | 23 | pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
vera1 | 0:bc949f735b8d | 24 | } |
vera1 | 0:bc949f735b8d | 25 | */ |
vera1 | 0:bc949f735b8d | 26 | volatile float potvalue = 0.0; |
vera1 | 0:bc949f735b8d | 27 | void readpot(){ |
vera1 | 0:bc949f735b8d | 28 | potvalue = pot.read(); |
vera1 | 0:bc949f735b8d | 29 | } |
vera1 | 0:bc949f735b8d | 30 | int main() |
vera1 | 0:bc949f735b8d | 31 | { |
vera1 | 0:bc949f735b8d | 32 | |
vera1 | 0:bc949f735b8d | 33 | //encoderreadout.attach(readoutencoder,0.2f); |
vera1 | 0:bc949f735b8d | 34 | pc.baud(9600); |
vera1 | 0:bc949f735b8d | 35 | potmeterreadout.attach(readpot,0.2f); |
vera1 | 0:bc949f735b8d | 36 | motorspeed.period(0.0001f); |
vera1 | 0:bc949f735b8d | 37 | //float motorspeed = 0.0f; |
vera1 | 0:bc949f735b8d | 38 | while (true) { |
vera1 | 0:bc949f735b8d | 39 | |
vera1 | 0:bc949f735b8d | 40 | led1 = potvalue; |
vera1 | 0:bc949f735b8d | 41 | motorspeed = potvalue; |
vera1 | 0:bc949f735b8d | 42 | pc.printf("reference velocity = %.2f\r\n", potvalue); |
vera1 | 0:bc949f735b8d | 43 | |
vera1 | 0:bc949f735b8d | 44 | |
vera1 | 0:bc949f735b8d | 45 | if ((button2 == 1)&&(button1 == 0)) { |
vera1 | 0:bc949f735b8d | 46 | |
vera1 | 0:bc949f735b8d | 47 | motorposition = 0; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 48 | led2 = 0; |
vera1 | 0:bc949f735b8d | 49 | |
vera1 | 0:bc949f735b8d | 50 | } |
vera1 | 0:bc949f735b8d | 51 | if ((button2 ==0)&&(button1 ==1)){ |
vera1 | 0:bc949f735b8d | 52 | |
vera1 | 0:bc949f735b8d | 53 | motorposition = 1; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 54 | led2 = 1; |
vera1 | 0:bc949f735b8d | 55 | } |
vera1 | 0:bc949f735b8d | 56 | //} |
vera1 | 0:bc949f735b8d | 57 | |
vera1 | 0:bc949f735b8d | 58 | } |
vera1 | 0:bc949f735b8d | 59 | } |
vera1 | 0:bc949f735b8d | 60 |