Ticker added

Dependencies:   HIDScope MODSERIAL QEI mbed

Fork of Tau_new_ticker_toevoegen by V D

Committer:
ARGroenenboom
Date:
Wed Nov 01 09:18:17 2017 +0000
Revision:
1:d2fe9abf5082
Parent:
0:cf88b023a080
Child:
2:9c3713420e24
Concept of calculating the motor angle to determine tau

Who changed what in which revision?

UserRevisionLine numberNew 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 0:cf88b023a080 23 float F = 0.02;
ARGroenenboom 1:d2fe9abf5082 24 float X = 32;
ARGroenenboom 1:d2fe9abf5082 25 float N = 131;
ARGroenenboom 1:d2fe9abf5082 26 float pulse_count;
ARGroenenboom 1:d2fe9abf5082 27 float angle_motor;
ARGroenenboom 1:d2fe9abf5082 28 QEI Encoder(A0,A1,NC,N);
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 1:d2fe9abf5082 37 pulse_count = Encoder.getPulses();
ARGroenenboom 0:cf88b023a080 38
ARGroenenboom 1:d2fe9abf5082 39 angle_pp =360/(X * N);
ARGroenenboom 1:d2fe9abf5082 40
ARGroenenboom 1:d2fe9abf5082 41 angle = pulse_count*angle_pp;
ARGroenenboom 0:cf88b023a080 42
ARGroenenboom 0:cf88b023a080 43 scope.set(0, angle);
ARGroenenboom 0:cf88b023a080 44 scope.send();
ARGroenenboom 0:cf88b023a080 45 }
ARGroenenboom 0:cf88b023a080 46
ARGroenenboom 0:cf88b023a080 47 //Dice rolling function
ARGroenenboom 0:cf88b023a080 48 void Roll()
ARGroenenboom 0:cf88b023a080 49 {
ARGroenenboom 0:cf88b023a080 50 ledg = 1; // green led off
ARGroenenboom 0:cf88b023a080 51 ledr = 0; // red led on = rolling
ARGroenenboom 0:cf88b023a080 52
ARGroenenboom 0:cf88b023a080 53 motor1PWM = 1; // motor on -> roll dice
ARGroenenboom 0:cf88b023a080 54 wait(1.5);
ARGroenenboom 0:cf88b023a080 55 motor1PWM = 0; // motor off
ARGroenenboom 0:cf88b023a080 56
ARGroenenboom 0:cf88b023a080 57 motor1DC = abs(motor1DC-1); // rotate other way next time
ARGroenenboom 0:cf88b023a080 58
ARGroenenboom 0:cf88b023a080 59 ledr = 1; // red led off
ARGroenenboom 0:cf88b023a080 60 ledb = 0; // blue led on
ARGroenenboom 1:d2fe9abf5082 61
ARGroenenboom 1:d2fe9abf5082 62 //Encoder.reset();
ARGroenenboom 0:cf88b023a080 63 }
ARGroenenboom 0:cf88b023a080 64
ARGroenenboom 0:cf88b023a080 65 int main()
ARGroenenboom 0:cf88b023a080 66 {
ARGroenenboom 0:cf88b023a080 67 motor1DC = 1;
ARGroenenboom 0:cf88b023a080 68 ledb = 1;
ARGroenenboom 0:cf88b023a080 69 ledr = 1;
ARGroenenboom 0:cf88b023a080 70 ledg = 0;
ARGroenenboom 0:cf88b023a080 71
ARGroenenboom 0:cf88b023a080 72 while (true)
ARGroenenboom 0:cf88b023a080 73 {
ARGroenenboom 0:cf88b023a080 74 if(button1==0)
ARGroenenboom 0:cf88b023a080 75 {
ARGroenenboom 0:cf88b023a080 76 Roll();
ARGroenenboom 0:cf88b023a080 77 wait(t); // wait before next roll to protect motors
ARGroenenboom 0:cf88b023a080 78 ledb = 1;
ARGroenenboom 0:cf88b023a080 79 ledg = 0; // green led on = ready to roll
ARGroenenboom 0:cf88b023a080 80 }
ARGroenenboom 0:cf88b023a080 81 Measure();
ARGroenenboom 0:cf88b023a080 82 }
ARGroenenboom 0:cf88b023a080 83 }