![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
tau new
Dependencies: HIDScope QEI mbed
Fork of Tau by
main.cpp@3:48397c80d0e4, 2017-11-01 (annotated)
- Committer:
- vd
- Date:
- Wed Nov 01 14:13:48 2017 +0000
- Revision:
- 3:48397c80d0e4
- Parent:
- 2:9c3713420e24
tau_new
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" |
ARGroenenboom | 0:cf88b023a080 | 5 | |
ARGroenenboom | 0:cf88b023a080 | 6 | //Leds |
ARGroenenboom | 0:cf88b023a080 | 7 | DigitalOut ledb(LED_BLUE); |
ARGroenenboom | 0:cf88b023a080 | 8 | DigitalOut ledr(LED_RED); |
ARGroenenboom | 0:cf88b023a080 | 9 | DigitalOut ledg(LED_GREEN); |
ARGroenenboom | 0:cf88b023a080 | 10 | |
ARGroenenboom | 0:cf88b023a080 | 11 | //Motor |
ARGroenenboom | 0:cf88b023a080 | 12 | DigitalOut motor1DC(D7); |
ARGroenenboom | 0:cf88b023a080 | 13 | PwmOut motor1PWM(D6); |
ARGroenenboom | 0:cf88b023a080 | 14 | |
ARGroenenboom | 0:cf88b023a080 | 15 | //Button |
ARGroenenboom | 0:cf88b023a080 | 16 | DigitalIn button1(SW2); |
ARGroenenboom | 0:cf88b023a080 | 17 | |
ARGroenenboom | 0:cf88b023a080 | 18 | // HIDscope |
ARGroenenboom | 0:cf88b023a080 | 19 | HIDScope scope(1); |
ARGroenenboom | 0:cf88b023a080 | 20 | |
ARGroenenboom | 0:cf88b023a080 | 21 | // Encoder |
ARGroenenboom | 1:d2fe9abf5082 | 22 | //Ticker tick; |
ARGroenenboom | 1:d2fe9abf5082 | 23 | float X = 32; |
ARGroenenboom | 1:d2fe9abf5082 | 24 | float N = 131; |
ARGroenenboom | 1:d2fe9abf5082 | 25 | float pulse_count; |
ARGroenenboom | 1:d2fe9abf5082 | 26 | float angle_motor; |
ARGroenenboom | 1:d2fe9abf5082 | 27 | QEI Encoder(A0,A1,NC,N); |
ARGroenenboom | 0:cf88b023a080 | 28 | |
ARGroenenboom | 0:cf88b023a080 | 29 | //Minimum wait time between rolls |
vd | 3:48397c80d0e4 | 30 | float t = 1; |
ARGroenenboom | 0:cf88b023a080 | 31 | |
ARGroenenboom | 0:cf88b023a080 | 32 | // Encoder measurement function |
ARGroenenboom | 0:cf88b023a080 | 33 | void Measure() |
ARGroenenboom | 0:cf88b023a080 | 34 | { |
ARGroenenboom | 0:cf88b023a080 | 35 | // Get pulses and send to HIDScope |
ARGroenenboom | 1:d2fe9abf5082 | 36 | pulse_count = Encoder.getPulses(); |
ARGroenenboom | 0:cf88b023a080 | 37 | |
vd | 2:9c3713420e24 | 38 | int angle_pp =360/(X * N); |
ARGroenenboom | 1:d2fe9abf5082 | 39 | |
vd | 2:9c3713420e24 | 40 | int angle = pulse_count*angle_pp; |
ARGroenenboom | 0:cf88b023a080 | 41 | |
ARGroenenboom | 0:cf88b023a080 | 42 | scope.set(0, angle); |
ARGroenenboom | 0:cf88b023a080 | 43 | scope.send(); |
ARGroenenboom | 0:cf88b023a080 | 44 | } |
ARGroenenboom | 0:cf88b023a080 | 45 | |
ARGroenenboom | 0:cf88b023a080 | 46 | //Dice rolling function |
ARGroenenboom | 0:cf88b023a080 | 47 | void Roll() |
ARGroenenboom | 0:cf88b023a080 | 48 | { |
vd | 3:48397c80d0e4 | 49 | //motor1DC = 0; |
vd | 3:48397c80d0e4 | 50 | |
ARGroenenboom | 0:cf88b023a080 | 51 | ledg = 1; // green led off |
ARGroenenboom | 0:cf88b023a080 | 52 | ledr = 0; // red led on = rolling |
ARGroenenboom | 0:cf88b023a080 | 53 | |
ARGroenenboom | 0:cf88b023a080 | 54 | motor1PWM = 1; // motor on -> roll dice |
vd | 3:48397c80d0e4 | 55 | wait(3.0); |
ARGroenenboom | 0:cf88b023a080 | 56 | motor1PWM = 0; // motor off |
ARGroenenboom | 0:cf88b023a080 | 57 | |
vd | 3:48397c80d0e4 | 58 | |
vd | 2:9c3713420e24 | 59 | //motor1DC = abs(motor1DC-1); // rotate other way next time |
ARGroenenboom | 0:cf88b023a080 | 60 | |
vd | 2:9c3713420e24 | 61 | //ledr = 1; // red led off |
vd | 2:9c3713420e24 | 62 | //ledb = 0; // blue led on |
ARGroenenboom | 1:d2fe9abf5082 | 63 | |
ARGroenenboom | 1:d2fe9abf5082 | 64 | //Encoder.reset(); |
ARGroenenboom | 0:cf88b023a080 | 65 | } |
ARGroenenboom | 0:cf88b023a080 | 66 | |
ARGroenenboom | 0:cf88b023a080 | 67 | int main() |
ARGroenenboom | 0:cf88b023a080 | 68 | { |
vd | 3:48397c80d0e4 | 69 | motor1DC = 0; |
ARGroenenboom | 0:cf88b023a080 | 70 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 71 | ledr = 1; |
ARGroenenboom | 0:cf88b023a080 | 72 | ledg = 0; |
ARGroenenboom | 0:cf88b023a080 | 73 | |
ARGroenenboom | 0:cf88b023a080 | 74 | while (true) |
ARGroenenboom | 0:cf88b023a080 | 75 | { |
ARGroenenboom | 0:cf88b023a080 | 76 | if(button1==0) |
ARGroenenboom | 0:cf88b023a080 | 77 | { |
ARGroenenboom | 0:cf88b023a080 | 78 | Roll(); |
ARGroenenboom | 0:cf88b023a080 | 79 | wait(t); // wait before next roll to protect motors |
ARGroenenboom | 0:cf88b023a080 | 80 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 81 | ledg = 0; // green led on = ready to roll |
ARGroenenboom | 0:cf88b023a080 | 82 | } |
ARGroenenboom | 0:cf88b023a080 | 83 | Measure(); |
ARGroenenboom | 0:cf88b023a080 | 84 | } |
ARGroenenboom | 0:cf88b023a080 | 85 | } |