Ticker added
Dependencies: HIDScope MODSERIAL QEI mbed
Fork of Tau_new_ticker_toevoegen by
main.cpp@6:d8c4c54e1077, 2017-11-02 (annotated)
- Committer:
- ARGroenenboom
- Date:
- Thu Nov 02 09:14:01 2017 +0000
- Revision:
- 6:d8c4c54e1077
- Parent:
- 5:9b5ab304fe64
- Child:
- 7:05bef8a64a44
Ticker hopefully in right place
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ARGroenenboom | 0:cf88b023a080 | 1 | #include "mbed.h" |
ARGroenenboom | 0:cf88b023a080 | 2 | #include "HIDScope.h" |
ARGroenenboom | 0:cf88b023a080 | 3 | #include "QEI.h" |
ARGroenenboom | 0:cf88b023a080 | 4 | #include "iostream" |
vd | 4:e22508afc194 | 5 | #include "MODSERIAL.h" |
ARGroenenboom | 0:cf88b023a080 | 6 | |
ARGroenenboom | 0:cf88b023a080 | 7 | //Leds |
ARGroenenboom | 0:cf88b023a080 | 8 | DigitalOut ledb(LED_BLUE); |
ARGroenenboom | 0:cf88b023a080 | 9 | DigitalOut ledr(LED_RED); |
ARGroenenboom | 0:cf88b023a080 | 10 | DigitalOut ledg(LED_GREEN); |
ARGroenenboom | 0:cf88b023a080 | 11 | |
ARGroenenboom | 0:cf88b023a080 | 12 | //Motor |
ARGroenenboom | 0:cf88b023a080 | 13 | DigitalOut motor1DC(D7); |
ARGroenenboom | 0:cf88b023a080 | 14 | PwmOut motor1PWM(D6); |
ARGroenenboom | 0:cf88b023a080 | 15 | |
ARGroenenboom | 0:cf88b023a080 | 16 | //Button |
ARGroenenboom | 0:cf88b023a080 | 17 | DigitalIn button1(SW2); |
ARGroenenboom | 0:cf88b023a080 | 18 | |
ARGroenenboom | 0:cf88b023a080 | 19 | // HIDscope |
ARGroenenboom | 0:cf88b023a080 | 20 | HIDScope scope(1); |
ARGroenenboom | 0:cf88b023a080 | 21 | |
vd | 4:e22508afc194 | 22 | //Ticker |
vd | 4:e22508afc194 | 23 | Ticker tick; |
vd | 4:e22508afc194 | 24 | float f = 0.2; |
vd | 4:e22508afc194 | 25 | |
ARGroenenboom | 0:cf88b023a080 | 26 | // Encoder |
vd | 4:e22508afc194 | 27 | float X = 32.0; |
vd | 4:e22508afc194 | 28 | float N = 131.0; |
ARGroenenboom | 1:d2fe9abf5082 | 29 | float pulse_count; |
ARGroenenboom | 1:d2fe9abf5082 | 30 | float angle_motor; |
vd | 4:e22508afc194 | 31 | QEI Encoder(D8,D9,NC,N); |
vd | 4:e22508afc194 | 32 | MODSERIAL pc(USBTX,USBRX); |
ARGroenenboom | 0:cf88b023a080 | 33 | |
ARGroenenboom | 5:9b5ab304fe64 | 34 | // Send angle to HIDScope and pc function |
ARGroenenboom | 5:9b5ab304fe64 | 35 | void Send() |
ARGroenenboom | 5:9b5ab304fe64 | 36 | { |
ARGroenenboom | 5:9b5ab304fe64 | 37 | // Get pulses and calculate angle |
ARGroenenboom | 5:9b5ab304fe64 | 38 | pulse_count = Encoder.getPulses(); |
ARGroenenboom | 5:9b5ab304fe64 | 39 | float angle_pp =360/(X * N); |
ARGroenenboom | 5:9b5ab304fe64 | 40 | float angle = pulse_count*angle_pp; |
ARGroenenboom | 5:9b5ab304fe64 | 41 | |
ARGroenenboom | 5:9b5ab304fe64 | 42 | // Send angle to HIDScope and pc |
ARGroenenboom | 5:9b5ab304fe64 | 43 | scope.set(0, angle); |
ARGroenenboom | 5:9b5ab304fe64 | 44 | scope.send(); |
ARGroenenboom | 5:9b5ab304fe64 | 45 | pc.printf(" pulse count: %f angle: %f \r \n ", pulse_count, angle); |
ARGroenenboom | 5:9b5ab304fe64 | 46 | |
ARGroenenboom | 5:9b5ab304fe64 | 47 | // Reset encoder to reset pules count |
ARGroenenboom | 5:9b5ab304fe64 | 48 | Encoder.reset(); |
ARGroenenboom | 5:9b5ab304fe64 | 49 | } |
ARGroenenboom | 5:9b5ab304fe64 | 50 | |
ARGroenenboom | 0:cf88b023a080 | 51 | |
ARGroenenboom | 5:9b5ab304fe64 | 52 | // Roll function |
ARGroenenboom | 5:9b5ab304fe64 | 53 | void Roll() |
ARGroenenboom | 5:9b5ab304fe64 | 54 | { |
ARGroenenboom | 6:d8c4c54e1077 | 55 | tick.attach(&Send, f); |
ARGroenenboom | 6:d8c4c54e1077 | 56 | |
ARGroenenboom | 5:9b5ab304fe64 | 57 | // Show rolling state through leds |
ARGroenenboom | 5:9b5ab304fe64 | 58 | ledg = 1; // green led off |
ARGroenenboom | 5:9b5ab304fe64 | 59 | ledb = 1; // blue led off |
ARGroenenboom | 5:9b5ab304fe64 | 60 | ledr = 0; // red led on = rolling |
ARGroenenboom | 0:cf88b023a080 | 61 | |
ARGroenenboom | 5:9b5ab304fe64 | 62 | // Turn motor on for x time and turn it off again |
ARGroenenboom | 5:9b5ab304fe64 | 63 | motor1PWM = 1; // motor on |
ARGroenenboom | 5:9b5ab304fe64 | 64 | wait(3.00); // Let motor turn for x time |
ARGroenenboom | 5:9b5ab304fe64 | 65 | motor1PWM = 0; // motor off |
ARGroenenboom | 5:9b5ab304fe64 | 66 | |
ARGroenenboom | 5:9b5ab304fe64 | 67 | // Show ready state through leds |
ARGroenenboom | 5:9b5ab304fe64 | 68 | wait(1.0); // wait before next roll to protect motors |
ARGroenenboom | 5:9b5ab304fe64 | 69 | ledr = 1; // red led off |
ARGroenenboom | 5:9b5ab304fe64 | 70 | ledb = 1; // blue led off |
ARGroenenboom | 5:9b5ab304fe64 | 71 | ledg = 0; // green led on = ready to roll |
ARGroenenboom | 5:9b5ab304fe64 | 72 | |
ARGroenenboom | 5:9b5ab304fe64 | 73 | } |
vd | 3:48397c80d0e4 | 74 | |
ARGroenenboom | 0:cf88b023a080 | 75 | |
ARGroenenboom | 0:cf88b023a080 | 76 | int main() |
ARGroenenboom | 0:cf88b023a080 | 77 | { |
ARGroenenboom | 5:9b5ab304fe64 | 78 | // Initialize system |
vd | 4:e22508afc194 | 79 | pc.baud(9600); |
vd | 4:e22508afc194 | 80 | motor1DC = -1; |
ARGroenenboom | 0:cf88b023a080 | 81 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 82 | ledr = 1; |
ARGroenenboom | 0:cf88b023a080 | 83 | ledg = 0; |
ARGroenenboom | 5:9b5ab304fe64 | 84 | |
ARGroenenboom | 5:9b5ab304fe64 | 85 | // Dice code |
ARGroenenboom | 5:9b5ab304fe64 | 86 | if(button1==false) |
ARGroenenboom | 5:9b5ab304fe64 | 87 | { |
ARGroenenboom | 5:9b5ab304fe64 | 88 | Roll(); |
ARGroenenboom | 5:9b5ab304fe64 | 89 | } |
ARGroenenboom | 6:d8c4c54e1077 | 90 | |
vd | 4:e22508afc194 | 91 | |
ARGroenenboom | 5:9b5ab304fe64 | 92 | // Perform main continuously |
ARGroenenboom | 5:9b5ab304fe64 | 93 | while(true) {} |
vd | 4:e22508afc194 | 94 | } |
vd | 4:e22508afc194 | 95 | |
vd | 4:e22508afc194 | 96 | // screen /dev/tty.usbmodem1412 9600 |