test with PID control to screen on your computer
Dependencies: Motor_with_encoder MODSERIAL QEI mbed
Fork of Project_motor by
main.cpp@1:e0f6cdefcd6e, 2017-10-03 (annotated)
- Committer:
- vera1
- Date:
- Tue Oct 03 15:24:46 2017 +0000
- Revision:
- 1:e0f6cdefcd6e
- Parent:
- 0:bc949f735b8d
- Child:
- 2:80177e90c1e6
start exercise 2
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 | 1:e0f6cdefcd6e | 10 | DigitalOut led2(D11); |
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 | 1:e0f6cdefcd6e | 15 | Ticker encoderreadout; |
vera1 | 1:e0f6cdefcd6e | 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 | 1:e0f6cdefcd6e | 27 | volatile float position = 0.0; |
vera1 | 0:bc949f735b8d | 28 | void readpot(){ |
vera1 | 0:bc949f735b8d | 29 | potvalue = pot.read(); |
vera1 | 1:e0f6cdefcd6e | 30 | //position = motor1.getPosition(); |
vera1 | 1:e0f6cdefcd6e | 31 | pc.printf("pos: %d, speed %f \r\n",motor1.getPosition(), motor1.getSpeed()); |
vera1 | 0:bc949f735b8d | 32 | } |
vera1 | 0:bc949f735b8d | 33 | int main() |
vera1 | 1:e0f6cdefcd6e | 34 | { |
vera1 | 0:bc949f735b8d | 35 | |
vera1 | 0:bc949f735b8d | 36 | pc.baud(9600); |
vera1 | 0:bc949f735b8d | 37 | potmeterreadout.attach(readpot,0.2f); |
vera1 | 0:bc949f735b8d | 38 | motorspeed.period(0.0001f); |
vera1 | 0:bc949f735b8d | 39 | //float motorspeed = 0.0f; |
vera1 | 0:bc949f735b8d | 40 | while (true) { |
vera1 | 0:bc949f735b8d | 41 | |
vera1 | 0:bc949f735b8d | 42 | led1 = potvalue; |
vera1 | 0:bc949f735b8d | 43 | motorspeed = potvalue; |
vera1 | 1:e0f6cdefcd6e | 44 | //pc.printf("reference velocity = %.2f\r\n", potvalue); |
vera1 | 0:bc949f735b8d | 45 | |
vera1 | 0:bc949f735b8d | 46 | |
vera1 | 0:bc949f735b8d | 47 | if ((button2 == 1)&&(button1 == 0)) { |
vera1 | 0:bc949f735b8d | 48 | |
vera1 | 0:bc949f735b8d | 49 | motorposition = 0; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 50 | led2 = 0; |
vera1 | 0:bc949f735b8d | 51 | |
vera1 | 0:bc949f735b8d | 52 | } |
vera1 | 0:bc949f735b8d | 53 | if ((button2 ==0)&&(button1 ==1)){ |
vera1 | 0:bc949f735b8d | 54 | |
vera1 | 0:bc949f735b8d | 55 | motorposition = 1; // motor turns anticlockwise |
vera1 | 0:bc949f735b8d | 56 | led2 = 1; |
vera1 | 0:bc949f735b8d | 57 | } |
vera1 | 0:bc949f735b8d | 58 | //} |
vera1 | 0:bc949f735b8d | 59 | |
vera1 | 0:bc949f735b8d | 60 | } |
vera1 | 0:bc949f735b8d | 61 | } |
vera1 | 0:bc949f735b8d | 62 |