Made by Tom Lankhorst but now without errors
Dependencies: Encoder HIDScope MODSERIAL mbed
Fork of positioncontrolpot by
main.cpp@0:6ffe287c9e4c, 2015-09-22 (annotated)
- Committer:
- Gerth
- Date:
- Tue Sep 22 11:52:38 2015 +0000
- Revision:
- 0:6ffe287c9e4c
- Child:
- 1:757aadb68807
with this simple program the outputshaft follows the potmeter;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Gerth | 0:6ffe287c9e4c | 1 | #include "mbed.h" |
Gerth | 0:6ffe287c9e4c | 2 | #include "HIDScope.h" |
Gerth | 0:6ffe287c9e4c | 3 | #include "encoder.h" |
Gerth | 0:6ffe287c9e4c | 4 | |
Gerth | 0:6ffe287c9e4c | 5 | //hidscope met gewenst aantal kanalen |
Gerth | 0:6ffe287c9e4c | 6 | HIDScope scope(2); |
Gerth | 0:6ffe287c9e4c | 7 | |
Gerth | 0:6ffe287c9e4c | 8 | //analoog in van potmeter 1 |
Gerth | 0:6ffe287c9e4c | 9 | AnalogIn pot1(A0); |
Gerth | 0:6ffe287c9e4c | 10 | |
Gerth | 0:6ffe287c9e4c | 11 | //signaal naar motor uit |
Gerth | 0:6ffe287c9e4c | 12 | DigitalOut motor1_direction(D7);// draairichting motor 1 (1 is CW ) |
Gerth | 0:6ffe287c9e4c | 13 | PwmOut motor1_speed_control(D6);//aanstuursnelheid motor 1 |
Gerth | 0:6ffe287c9e4c | 14 | //PwmOut motor2_speed_control(D5); |
Gerth | 0:6ffe287c9e4c | 15 | //DigitalOut motor2_direction(D4); |
Gerth | 0:6ffe287c9e4c | 16 | |
Gerth | 0:6ffe287c9e4c | 17 | //encoders |
Gerth | 0:6ffe287c9e4c | 18 | Encoder motor1_encoder(D13,D12); |
Gerth | 0:6ffe287c9e4c | 19 | //Encoder motor2_encoder(D11,D10); |
Gerth | 0:6ffe287c9e4c | 20 | |
Gerth | 0:6ffe287c9e4c | 21 | //tickers |
Gerth | 0:6ffe287c9e4c | 22 | Ticker scopedataticker; |
Gerth | 0:6ffe287c9e4c | 23 | Ticker adjust_positionticker; |
Gerth | 0:6ffe287c9e4c | 24 | |
Gerth | 0:6ffe287c9e4c | 25 | //frequenties |
Gerth | 0:6ffe287c9e4c | 26 | const float motor_frequency_pwm = 1000; //1kHz PWM |
Gerth | 0:6ffe287c9e4c | 27 | const float scopedatafrequency=50;// frequentie waarmee informatie naar de scope gestuurd wordt |
Gerth | 0:6ffe287c9e4c | 28 | const float adjust_position_frequency=8; // frequentie waarmee de motorpositie aangepast wordt |
Gerth | 0:6ffe287c9e4c | 29 | |
Gerth | 0:6ffe287c9e4c | 30 | //constanten |
Gerth | 0:6ffe287c9e4c | 31 | const float cpr=4200; |
Gerth | 0:6ffe287c9e4c | 32 | |
Gerth | 0:6ffe287c9e4c | 33 | |
Gerth | 0:6ffe287c9e4c | 34 | //go flags |
Gerth | 0:6ffe287c9e4c | 35 | bool scopedata_go=false; |
Gerth | 0:6ffe287c9e4c | 36 | bool adjust_position_go=false; |
Gerth | 0:6ffe287c9e4c | 37 | |
Gerth | 0:6ffe287c9e4c | 38 | //activators |
Gerth | 0:6ffe287c9e4c | 39 | void scopedata_activate() |
Gerth | 0:6ffe287c9e4c | 40 | { |
Gerth | 0:6ffe287c9e4c | 41 | scopedata_go=true; |
Gerth | 0:6ffe287c9e4c | 42 | } |
Gerth | 0:6ffe287c9e4c | 43 | void adjust_position_activate() |
Gerth | 0:6ffe287c9e4c | 44 | { |
Gerth | 0:6ffe287c9e4c | 45 | adjust_position_go=true; |
Gerth | 0:6ffe287c9e4c | 46 | } |
Gerth | 0:6ffe287c9e4c | 47 | |
Gerth | 0:6ffe287c9e4c | 48 | //scopefunctie |
Gerth | 0:6ffe287c9e4c | 49 | void scopedata() |
Gerth | 0:6ffe287c9e4c | 50 | { |
Gerth | 0:6ffe287c9e4c | 51 | scope.set(0,pot1.read()); |
Gerth | 0:6ffe287c9e4c | 52 | scope.set(1,motor1_encoder.getPosition()); |
Gerth | 0:6ffe287c9e4c | 53 | scope.send(); |
Gerth | 0:6ffe287c9e4c | 54 | } |
Gerth | 0:6ffe287c9e4c | 55 | |
Gerth | 0:6ffe287c9e4c | 56 | //adjust position |
Gerth | 0:6ffe287c9e4c | 57 | void adjust_position() |
Gerth | 0:6ffe287c9e4c | 58 | { |
Gerth | 0:6ffe287c9e4c | 59 | float wantedposition=cpr*(pot1.read()); |
Gerth | 0:6ffe287c9e4c | 60 | int actualposition=(motor1_encoder.getPosition()); |
Gerth | 0:6ffe287c9e4c | 61 | if (wantedposition<=actualposition) { |
Gerth | 0:6ffe287c9e4c | 62 | motor1_direction=0; |
Gerth | 0:6ffe287c9e4c | 63 | motor1_speed_control=0.5; |
Gerth | 0:6ffe287c9e4c | 64 | } else if (wantedposition>=actualposition) { |
Gerth | 0:6ffe287c9e4c | 65 | motor1_direction=1; |
Gerth | 0:6ffe287c9e4c | 66 | motor1_speed_control=0.5; |
Gerth | 0:6ffe287c9e4c | 67 | } else { |
Gerth | 0:6ffe287c9e4c | 68 | motor1_direction=1; |
Gerth | 0:6ffe287c9e4c | 69 | motor1_speed_control=0; |
Gerth | 0:6ffe287c9e4c | 70 | } |
Gerth | 0:6ffe287c9e4c | 71 | } |
Gerth | 0:6ffe287c9e4c | 72 | |
Gerth | 0:6ffe287c9e4c | 73 | int main () |
Gerth | 0:6ffe287c9e4c | 74 | { |
Gerth | 0:6ffe287c9e4c | 75 | motor1_speed_control.period(1/motor_frequency_pwm); |
Gerth | 0:6ffe287c9e4c | 76 | |
Gerth | 0:6ffe287c9e4c | 77 | scopedataticker.attach(&scopedata_activate,1/scopedatafrequency); |
Gerth | 0:6ffe287c9e4c | 78 | adjust_positionticker.attach(&adjust_position_activate,1/adjust_position_frequency); |
Gerth | 0:6ffe287c9e4c | 79 | |
Gerth | 0:6ffe287c9e4c | 80 | while(1) { |
Gerth | 0:6ffe287c9e4c | 81 | if (scopedata_go==true) { |
Gerth | 0:6ffe287c9e4c | 82 | scopedata(); |
Gerth | 0:6ffe287c9e4c | 83 | scopedata_go=false; |
Gerth | 0:6ffe287c9e4c | 84 | } |
Gerth | 0:6ffe287c9e4c | 85 | if (adjust_position_go==true) { |
Gerth | 0:6ffe287c9e4c | 86 | adjust_position(); |
Gerth | 0:6ffe287c9e4c | 87 | adjust_position_go=false; |
Gerth | 0:6ffe287c9e4c | 88 | |
Gerth | 0:6ffe287c9e4c | 89 | } |
Gerth | 0:6ffe287c9e4c | 90 | } |
Gerth | 0:6ffe287c9e4c | 91 | } |