Reads the encoder signals from 1 motor and displays the counts and degrees of the output shaft in Serial.

Dependencies:   HIDScope_motor_ff QEI mbed FastPWM MODSERIAL

Fork of HID_scope_test by Biorobotics_group_2

Committer:
sjoerdbarts
Date:
Fri Oct 07 12:28:01 2016 +0000
Revision:
7:e7aa4f10d1fb
Parent:
6:ed11342ab079
Child:
8:fe907b9a0bab
Additional comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sjoerdbarts 0:1883abafaa19 1 #include "mbed.h"
sjoerdbarts 2:5fce9d33997f 2 #include "HIDScope.h"
sjoerdbarts 5:36788b154e25 3 #include "QEI.h"
sjoerdbarts 3:ce0f979f15fb 4 #define SERIAL_BAUD 115200 // baud rate for serial communication
sjoerdbarts 3:ce0f979f15fb 5
sjoerdbarts 5:36788b154e25 6 Serial pc(USBTX,USBRX);
sjoerdbarts 3:ce0f979f15fb 7
sjoerdbarts 7:e7aa4f10d1fb 8 // Variables counter
sjoerdbarts 5:36788b154e25 9 int countsCW = 0;
sjoerdbarts 5:36788b154e25 10 int countsCCW = 0;
sjoerdbarts 5:36788b154e25 11 int net_counts = 0;
sjoerdbarts 7:e7aa4f10d1fb 12
sjoerdbarts 7:e7aa4f10d1fb 13 // Variables degrees, and speed
sjoerdbarts 5:36788b154e25 14 float degrees = 0.0;
sjoerdbarts 6:ed11342ab079 15 volatile float curr_degrees = 0.0;
sjoerdbarts 6:ed11342ab079 16 volatile float prev_degrees = 0.0;
sjoerdbarts 6:ed11342ab079 17 volatile float speed = 0.0; // speed in degrees/s
sjoerdbarts 6:ed11342ab079 18 volatile const float T_CalculateSpeed = 0.1; // 100 Hz
sjoerdbarts 6:ed11342ab079 19
sjoerdbarts 7:e7aa4f10d1fb 20 // Set counts per revolution
sjoerdbarts 5:36788b154e25 21 const float counts_per_rev = 4200.0;
sjoerdbarts 7:e7aa4f10d1fb 22
sjoerdbarts 7:e7aa4f10d1fb 23 // Set encoder
sjoerdbarts 5:36788b154e25 24 QEI EncoderCW(D12,D13,NC,32);
sjoerdbarts 5:36788b154e25 25 QEI EncoderCCW(D13,D12,NC,32);
sjoerdbarts 0:1883abafaa19 26
sjoerdbarts 7:e7aa4f10d1fb 27 // Print the output
sjoerdbarts 5:36788b154e25 28 void PrintDegrees(){
sjoerdbarts 5:36788b154e25 29 pc.printf("\r\n Nett Pulses %i \r\n", net_counts);
sjoerdbarts 5:36788b154e25 30 pc.printf("\r\n Output degrees %f \r\n", degrees);
sjoerdbarts 6:ed11342ab079 31 pc.printf("\r\n Speed %f \r\n",speed);
sjoerdbarts 5:36788b154e25 32 }
sjoerdbarts 7:e7aa4f10d1fb 33
sjoerdbarts 7:e7aa4f10d1fb 34 // Calculate the speed
sjoerdbarts 6:ed11342ab079 35 void CalculateSpeed() {
sjoerdbarts 6:ed11342ab079 36 curr_degrees = degrees;
sjoerdbarts 6:ed11342ab079 37 speed = (curr_degrees-prev_degrees)/T_CalculateSpeed;
sjoerdbarts 6:ed11342ab079 38 prev_degrees = curr_degrees;
sjoerdbarts 6:ed11342ab079 39 }
sjoerdbarts 5:36788b154e25 40
sjoerdbarts 0:1883abafaa19 41 int main()
sjoerdbarts 0:1883abafaa19 42 {
sjoerdbarts 3:ce0f979f15fb 43 pc.baud(SERIAL_BAUD);
sjoerdbarts 3:ce0f979f15fb 44 pc.printf("\r\n ***THERMONUCLEAR WARFARE COMMENCES*** \r\n");
sjoerdbarts 3:ce0f979f15fb 45
sjoerdbarts 5:36788b154e25 46 // Set ticker for serial communication of counts and degrees
sjoerdbarts 5:36788b154e25 47 Ticker PrintDegreesTicker;
sjoerdbarts 5:36788b154e25 48 PrintDegreesTicker.attach(&PrintDegrees,0.1);
sjoerdbarts 2:5fce9d33997f 49
sjoerdbarts 6:ed11342ab079 50 // Set ticker for speed calculation
sjoerdbarts 6:ed11342ab079 51 Ticker CalculateSpeedTicker;
sjoerdbarts 6:ed11342ab079 52 CalculateSpeedTicker.attach(CalculateSpeed,T_CalculateSpeed);
sjoerdbarts 6:ed11342ab079 53
sjoerdbarts 5:36788b154e25 54 // count the CW and CCW counts and calculate the output degrees
sjoerdbarts 5:36788b154e25 55 while(true){
sjoerdbarts 5:36788b154e25 56 countsCW = EncoderCW.getPulses();
sjoerdbarts 5:36788b154e25 57 countsCCW= EncoderCCW.getPulses();
sjoerdbarts 5:36788b154e25 58 net_counts=countsCW-countsCCW;
sjoerdbarts 5:36788b154e25 59 degrees=(net_counts*360.0)/counts_per_rev;
sjoerdbarts 5:36788b154e25 60
sjoerdbarts 5:36788b154e25 61 }
sjoerdbarts 0:1883abafaa19 62 }