BioRobotics - Polulu 1447 Concept version of determining the motor angle to determine tau
Dependencies: HIDScope QEI mbed
main.cpp@0:cf88b023a080, 2017-10-31 (annotated)
- Committer:
- ARGroenenboom
- Date:
- Tue Oct 31 13:51:16 2017 +0000
- Revision:
- 0:cf88b023a080
- Child:
- 1:d2fe9abf5082
Working. Roll and send encoder values to HIDScope. Strange values on HIDScope.
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 | //PC |
ARGroenenboom | 0:cf88b023a080 | 19 | Serial pc(USBTX, USBRX); |
ARGroenenboom | 0:cf88b023a080 | 20 | |
ARGroenenboom | 0:cf88b023a080 | 21 | // HIDscope |
ARGroenenboom | 0:cf88b023a080 | 22 | HIDScope scope(1); |
ARGroenenboom | 0:cf88b023a080 | 23 | |
ARGroenenboom | 0:cf88b023a080 | 24 | // Encoder |
ARGroenenboom | 0:cf88b023a080 | 25 | int counts; |
ARGroenenboom | 0:cf88b023a080 | 26 | QEI Encoder(A0,A1,NC,32); |
ARGroenenboom | 0:cf88b023a080 | 27 | Ticker tick; |
ARGroenenboom | 0:cf88b023a080 | 28 | float F = 0.02; |
ARGroenenboom | 0:cf88b023a080 | 29 | |
ARGroenenboom | 0:cf88b023a080 | 30 | //Minimum wait time between rolls |
ARGroenenboom | 0:cf88b023a080 | 31 | float t = 1; |
ARGroenenboom | 0:cf88b023a080 | 32 | |
ARGroenenboom | 0:cf88b023a080 | 33 | // Encoder measurement function |
ARGroenenboom | 0:cf88b023a080 | 34 | void Measure() |
ARGroenenboom | 0:cf88b023a080 | 35 | { |
ARGroenenboom | 0:cf88b023a080 | 36 | // Get pulses and send to HIDScope |
ARGroenenboom | 0:cf88b023a080 | 37 | counts = Encoder.getPulses(); |
ARGroenenboom | 0:cf88b023a080 | 38 | |
ARGroenenboom | 0:cf88b023a080 | 39 | float angle_pc = 1/87; |
ARGroenenboom | 0:cf88b023a080 | 40 | float angle = counts * angle_pc; |
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 | pc.printf("Counts:%i. \n Angle:%i. \n",counts,angle); |
ARGroenenboom | 0:cf88b023a080 | 46 | } |
ARGroenenboom | 0:cf88b023a080 | 47 | |
ARGroenenboom | 0:cf88b023a080 | 48 | //Dice rolling function |
ARGroenenboom | 0:cf88b023a080 | 49 | void Roll() |
ARGroenenboom | 0:cf88b023a080 | 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 |
ARGroenenboom | 0:cf88b023a080 | 55 | wait(1.5); |
ARGroenenboom | 0:cf88b023a080 | 56 | motor1PWM = 0; // motor off |
ARGroenenboom | 0:cf88b023a080 | 57 | |
ARGroenenboom | 0:cf88b023a080 | 58 | motor1DC = abs(motor1DC-1); // rotate other way next time |
ARGroenenboom | 0:cf88b023a080 | 59 | |
ARGroenenboom | 0:cf88b023a080 | 60 | ledr = 1; // red led off |
ARGroenenboom | 0:cf88b023a080 | 61 | ledb = 0; // blue led on |
ARGroenenboom | 0:cf88b023a080 | 62 | } |
ARGroenenboom | 0:cf88b023a080 | 63 | |
ARGroenenboom | 0:cf88b023a080 | 64 | int main() |
ARGroenenboom | 0:cf88b023a080 | 65 | { |
ARGroenenboom | 0:cf88b023a080 | 66 | motor1DC = 1; |
ARGroenenboom | 0:cf88b023a080 | 67 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 68 | ledr = 1; |
ARGroenenboom | 0:cf88b023a080 | 69 | ledg = 0; |
ARGroenenboom | 0:cf88b023a080 | 70 | |
ARGroenenboom | 0:cf88b023a080 | 71 | |
ARGroenenboom | 0:cf88b023a080 | 72 | |
ARGroenenboom | 0:cf88b023a080 | 73 | while (true) |
ARGroenenboom | 0:cf88b023a080 | 74 | { |
ARGroenenboom | 0:cf88b023a080 | 75 | if(button1==0) |
ARGroenenboom | 0:cf88b023a080 | 76 | { |
ARGroenenboom | 0:cf88b023a080 | 77 | Roll(); |
ARGroenenboom | 0:cf88b023a080 | 78 | wait(t); // wait before next roll to protect motors |
ARGroenenboom | 0:cf88b023a080 | 79 | ledb = 1; |
ARGroenenboom | 0:cf88b023a080 | 80 | ledg = 0; // green led on = ready to roll |
ARGroenenboom | 0:cf88b023a080 | 81 | } |
ARGroenenboom | 0:cf88b023a080 | 82 | Measure(); |
ARGroenenboom | 0:cf88b023a080 | 83 | } |
ARGroenenboom | 0:cf88b023a080 | 84 | } |