Ticker added
Dependencies: HIDScope MODSERIAL QEI mbed
Fork of Tau_new_ticker_toevoegen by
main.cpp@10:cab7ca2f144a, 2017-11-02 (annotated)
- Committer:
- ARGroenenboom
- Date:
- Thu Nov 02 12:02:01 2017 +0000
- Revision:
- 10:cab7ca2f144a
- Parent:
- 9:23fa7b03e2fe
- Child:
- 11:6138cfca0081
(Almost) Perfect.
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; |
ARGroenenboom | 9:23fa7b03e2fe | 24 | float R = 3; |
ARGroenenboom | 9:23fa7b03e2fe | 25 | float F = 0.2; |
ARGroenenboom | 7:05bef8a64a44 | 26 | int counter=0; |
vd | 4:e22508afc194 | 27 | |
ARGroenenboom | 0:cf88b023a080 | 28 | // Encoder |
vd | 4:e22508afc194 | 29 | float X = 32.0; |
vd | 4:e22508afc194 | 30 | float N = 131.0; |
ARGroenenboom | 1:d2fe9abf5082 | 31 | float pulse_count; |
ARGroenenboom | 1:d2fe9abf5082 | 32 | float angle_motor; |
vd | 4:e22508afc194 | 33 | QEI Encoder(D8,D9,NC,N); |
vd | 4:e22508afc194 | 34 | MODSERIAL pc(USBTX,USBRX); |
ARGroenenboom | 0:cf88b023a080 | 35 | |
ARGroenenboom | 5:9b5ab304fe64 | 36 | // Send angle to HIDScope and pc function |
ARGroenenboom | 8:62c7083f12d9 | 37 | void Calculate() |
ARGroenenboom | 5:9b5ab304fe64 | 38 | { |
ARGroenenboom | 5:9b5ab304fe64 | 39 | // Get pulses and calculate angle |
ARGroenenboom | 5:9b5ab304fe64 | 40 | pulse_count = Encoder.getPulses(); |
ARGroenenboom | 5:9b5ab304fe64 | 41 | float angle_pp =360/(X * N); |
ARGroenenboom | 5:9b5ab304fe64 | 42 | float angle = pulse_count*angle_pp; |
ARGroenenboom | 5:9b5ab304fe64 | 43 | |
ARGroenenboom | 5:9b5ab304fe64 | 44 | // Send angle to HIDScope and pc |
ARGroenenboom | 5:9b5ab304fe64 | 45 | scope.set(0, angle); |
ARGroenenboom | 5:9b5ab304fe64 | 46 | scope.send(); |
ARGroenenboom | 5:9b5ab304fe64 | 47 | pc.printf(" pulse count: %f angle: %f \r \n ", pulse_count, angle); |
ARGroenenboom | 5:9b5ab304fe64 | 48 | |
ARGroenenboom | 7:05bef8a64a44 | 49 | counter++; |
ARGroenenboom | 7:05bef8a64a44 | 50 | |
ARGroenenboom | 7:05bef8a64a44 | 51 | // Turn motor off and reset encoder to reset pules count |
ARGroenenboom | 7:05bef8a64a44 | 52 | |
ARGroenenboom | 9:23fa7b03e2fe | 53 | if(counter==R/F) |
ARGroenenboom | 7:05bef8a64a44 | 54 | { |
ARGroenenboom | 9:23fa7b03e2fe | 55 | motor1PWM = 0; // motor off |
ARGroenenboom | 9:23fa7b03e2fe | 56 | } |
ARGroenenboom | 5:9b5ab304fe64 | 57 | } |
ARGroenenboom | 5:9b5ab304fe64 | 58 | |
ARGroenenboom | 8:62c7083f12d9 | 59 | void Measure() |
ARGroenenboom | 8:62c7083f12d9 | 60 | { |
ARGroenenboom | 9:23fa7b03e2fe | 61 | tick.attach(&Calculate, F); |
ARGroenenboom | 8:62c7083f12d9 | 62 | } |
ARGroenenboom | 0:cf88b023a080 | 63 | |
ARGroenenboom | 5:9b5ab304fe64 | 64 | // Roll function |
ARGroenenboom | 5:9b5ab304fe64 | 65 | void Roll() |
ARGroenenboom | 5:9b5ab304fe64 | 66 | { |
ARGroenenboom | 5:9b5ab304fe64 | 67 | // Show rolling state through leds |
ARGroenenboom | 5:9b5ab304fe64 | 68 | ledg = 1; // green led off |
ARGroenenboom | 5:9b5ab304fe64 | 69 | ledb = 1; // blue led off |
ARGroenenboom | 5:9b5ab304fe64 | 70 | ledr = 0; // red led on = rolling |
ARGroenenboom | 0:cf88b023a080 | 71 | |
ARGroenenboom | 5:9b5ab304fe64 | 72 | // Turn motor on for x time and turn it off again |
ARGroenenboom | 5:9b5ab304fe64 | 73 | motor1PWM = 1; // motor on |
ARGroenenboom | 8:62c7083f12d9 | 74 | |
ARGroenenboom | 9:23fa7b03e2fe | 75 | // Reset counter and start measuring |
ARGroenenboom | 9:23fa7b03e2fe | 76 | Encoder.reset(); |
ARGroenenboom | 9:23fa7b03e2fe | 77 | counter = 0; |
ARGroenenboom | 9:23fa7b03e2fe | 78 | Measure(); |
ARGroenenboom | 5:9b5ab304fe64 | 79 | } |
vd | 3:48397c80d0e4 | 80 | |
ARGroenenboom | 0:cf88b023a080 | 81 | |
ARGroenenboom | 0:cf88b023a080 | 82 | int main() |
ARGroenenboom | 0:cf88b023a080 | 83 | { |
ARGroenenboom | 5:9b5ab304fe64 | 84 | // Initialize system |
vd | 4:e22508afc194 | 85 | pc.baud(9600); |
vd | 4:e22508afc194 | 86 | motor1DC = -1; |
ARGroenenboom | 0:cf88b023a080 | 87 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 88 | ledr = 1; |
ARGroenenboom | 0:cf88b023a080 | 89 | ledg = 0; |
ARGroenenboom | 5:9b5ab304fe64 | 90 | |
ARGroenenboom | 9:23fa7b03e2fe | 91 | // Dice roll code |
ARGroenenboom | 10:cab7ca2f144a | 92 | while(true){ |
ARGroenenboom | 9:23fa7b03e2fe | 93 | // Roll dice if button is pressed |
ARGroenenboom | 9:23fa7b03e2fe | 94 | if(button1==0) |
ARGroenenboom | 9:23fa7b03e2fe | 95 | { |
ARGroenenboom | 9:23fa7b03e2fe | 96 | Encoder.reset(); |
ARGroenenboom | 9:23fa7b03e2fe | 97 | Roll(); |
ARGroenenboom | 9:23fa7b03e2fe | 98 | } |
ARGroenenboom | 5:9b5ab304fe64 | 99 | } |
ARGroenenboom | 9:23fa7b03e2fe | 100 | } |