Ticker added

Dependencies:   HIDScope MODSERIAL QEI mbed

Fork of Tau_new_ticker_toevoegen by V D

Committer:
ARGroenenboom
Date:
Thu Nov 02 12:17:59 2017 +0000
Revision:
11:6138cfca0081
Parent:
10:cab7ca2f144a
Child:
12:61f24281f9a3
Perfect. Steps explained.

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"
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 11:6138cfca0081 39 // Get number of 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 11:6138cfca0081 44 // Send angle to HIDScope
ARGroenenboom 5:9b5ab304fe64 45 scope.set(0, angle);
ARGroenenboom 5:9b5ab304fe64 46 scope.send();
ARGroenenboom 5:9b5ab304fe64 47
ARGroenenboom 11:6138cfca0081 48 //Increase counter after measurement
ARGroenenboom 7:05bef8a64a44 49 counter++;
ARGroenenboom 7:05bef8a64a44 50
ARGroenenboom 11:6138cfca0081 51 // Turn motor off and reset encoder if desired rotating time has been reached
ARGroenenboom 11:6138cfca0081 52 // Number of measurements required to reach the desired number of rotations = Rotations / Measurement Frequency = (R/F)
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 11:6138cfca0081 72 // Turn motor on
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 int main()
ARGroenenboom 0:cf88b023a080 82 {
ARGroenenboom 5:9b5ab304fe64 83 // Initialize system
vd 4:e22508afc194 84 pc.baud(9600);
vd 4:e22508afc194 85 motor1DC = -1;
ARGroenenboom 0:cf88b023a080 86 ledb = 1;
ARGroenenboom 0:cf88b023a080 87 ledr = 1;
ARGroenenboom 0:cf88b023a080 88 ledg = 0;
ARGroenenboom 5:9b5ab304fe64 89
ARGroenenboom 9:23fa7b03e2fe 90 // Dice roll code
ARGroenenboom 10:cab7ca2f144a 91 while(true){
ARGroenenboom 11:6138cfca0081 92 // Roll dice if switch2 on K64F is pressed
ARGroenenboom 9:23fa7b03e2fe 93 if(button1==0)
ARGroenenboom 9:23fa7b03e2fe 94 {
ARGroenenboom 9:23fa7b03e2fe 95 Encoder.reset();
ARGroenenboom 9:23fa7b03e2fe 96 Roll();
ARGroenenboom 9:23fa7b03e2fe 97 }
ARGroenenboom 5:9b5ab304fe64 98 }
ARGroenenboom 9:23fa7b03e2fe 99 }