Ticker added
Dependencies: HIDScope MODSERIAL QEI mbed
Fork of Tau_new_ticker_toevoegen by
main.cpp@5:9b5ab304fe64, 2017-11-02 (annotated)
- Committer:
- ARGroenenboom
- Date:
- Thu Nov 02 09:09:28 2017 +0000
- Revision:
- 5:9b5ab304fe64
- Parent:
- 4:e22508afc194
- Child:
- 6:d8c4c54e1077
Ticker added
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 | 5:9b5ab304fe64 | 55 | // Show rolling state through leds |
ARGroenenboom | 5:9b5ab304fe64 | 56 | ledg = 1; // green led off |
ARGroenenboom | 5:9b5ab304fe64 | 57 | ledb = 1; // blue led off |
ARGroenenboom | 5:9b5ab304fe64 | 58 | ledr = 0; // red led on = rolling |
ARGroenenboom | 0:cf88b023a080 | 59 | |
ARGroenenboom | 5:9b5ab304fe64 | 60 | // Turn motor on for x time and turn it off again |
ARGroenenboom | 5:9b5ab304fe64 | 61 | motor1PWM = 1; // motor on |
ARGroenenboom | 5:9b5ab304fe64 | 62 | wait(3.00); // Let motor turn for x time |
ARGroenenboom | 5:9b5ab304fe64 | 63 | motor1PWM = 0; // motor off |
ARGroenenboom | 5:9b5ab304fe64 | 64 | |
ARGroenenboom | 5:9b5ab304fe64 | 65 | // Show ready state through leds |
ARGroenenboom | 5:9b5ab304fe64 | 66 | wait(1.0); // wait before next roll to protect motors |
ARGroenenboom | 5:9b5ab304fe64 | 67 | ledr = 1; // red led off |
ARGroenenboom | 5:9b5ab304fe64 | 68 | ledb = 1; // blue led off |
ARGroenenboom | 5:9b5ab304fe64 | 69 | ledg = 0; // green led on = ready to roll |
ARGroenenboom | 5:9b5ab304fe64 | 70 | |
ARGroenenboom | 5:9b5ab304fe64 | 71 | } |
vd | 3:48397c80d0e4 | 72 | |
ARGroenenboom | 0:cf88b023a080 | 73 | |
ARGroenenboom | 0:cf88b023a080 | 74 | int main() |
ARGroenenboom | 0:cf88b023a080 | 75 | { |
ARGroenenboom | 5:9b5ab304fe64 | 76 | // Initialize system |
vd | 4:e22508afc194 | 77 | pc.baud(9600); |
vd | 4:e22508afc194 | 78 | motor1DC = -1; |
ARGroenenboom | 0:cf88b023a080 | 79 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 80 | ledr = 1; |
ARGroenenboom | 0:cf88b023a080 | 81 | ledg = 0; |
ARGroenenboom | 5:9b5ab304fe64 | 82 | |
ARGroenenboom | 5:9b5ab304fe64 | 83 | // Dice code |
ARGroenenboom | 5:9b5ab304fe64 | 84 | if(button1==false) |
ARGroenenboom | 5:9b5ab304fe64 | 85 | { |
ARGroenenboom | 5:9b5ab304fe64 | 86 | Roll(); |
ARGroenenboom | 5:9b5ab304fe64 | 87 | } |
ARGroenenboom | 5:9b5ab304fe64 | 88 | tick.attach(&Send, f); |
vd | 4:e22508afc194 | 89 | |
ARGroenenboom | 5:9b5ab304fe64 | 90 | // Perform main continuously |
ARGroenenboom | 5:9b5ab304fe64 | 91 | while(true) {} |
vd | 4:e22508afc194 | 92 | } |
vd | 4:e22508afc194 | 93 | |
vd | 4:e22508afc194 | 94 | // screen /dev/tty.usbmodem1412 9600 |