BioRobotics - Polulu 1447 Concept version of determining the motor angle to determine tau

Dependencies:   HIDScope QEI mbed

Files at this revision

API Documentation at this revision

Comitter:
ARGroenenboom
Date:
Thu Nov 02 17:24:04 2017 +0000
Parent:
1:d2fe9abf5082
Commit message:
Perfect. Niet meer aankomen.

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed Nov 01 09:18:17 2017 +0000
+++ b/main.cpp	Thu Nov 02 17:24:04 2017 +0000
@@ -11,73 +11,107 @@
 //Motor
 DigitalOut motor1DC(D7);
 PwmOut motor1PWM(D6);
+bool On;
+bool reset;
 
 //Button
 DigitalIn   button1(SW2);
 
 // HIDscope
-HIDScope scope(1);
+HIDScope scope(2);
 
 // Encoder
-//Ticker tick;
-float F = 0.02;
 float X = 32;
 float N = 131;
 float pulse_count;
-float angle_motor;
-QEI Encoder(A0,A1,NC,N);
+QEI Encoder(D8,D9,NC,N);
+
+//Ticker function
+Ticker tick;
+double R = 30.0;
+double F = 0.01;
+double counter;
 
 //Minimum wait time between rolls
-float t = 1;
+//float t = 1;
 
 // Encoder measurement function
 void Measure()
 {
-    // Get pulses and send to HIDScope
+    // Get number of pulses and calculate angle
     pulse_count = Encoder.getPulses();
-    
-    angle_pp =360/(X * N);
+    float angle_pp =360/(X * N);
+    float angle = pulse_count*angle_pp;
+
+    // Send angle to HIDScope
+    scope.set(0, angle);
+    scope.set(1, counter);
+    scope.send();
     
-    angle = pulse_count*angle_pp;
+    // Turn motor off if desired rotation time has been reached
     
-    scope.set(0, angle);
-    scope.send();
+    if(angle/360.0>=1){
+        On=false;
+        counter++;
+        if(counter==2.0/F){
+        reset=true;
+        }
+    }
 }
 
 //Dice rolling function
 void Roll()
 {  
-    ledg = 1; // green led off
-    ledr = 0; // red led on = rolling
-    
-    motor1PWM = 1; // motor on -> roll dice
-    wait(1.5);
-    motor1PWM = 0; // motor off
+    if(button1==0){
+        On=true;
+    }
+    if(On==true){
+        // Roll dice
+        motor1PWM = 1; // motor on -> roll dice
+        
+        // Indicate rolling state
+        ledg = 1; 
+        ledb = 1; 
+        ledr = 0; // red led on
+        
 
-    motor1DC = abs(motor1DC-1); // rotate other way next time
-    
-    ledr = 1; // red led off
-    ledb = 0; // blue led on
-    
-    //Encoder.reset();
+    }
+    else{
+    // Stop rolling dice
+        motor1PWM = 0; // motor off
+        //motor1DC = abs(motor1DC-1); // rotate other way next time    
+        
+        // Indicate protection state
+        ledr = 1; 
+        ledg = 1;
+        ledb = 0; // blue led on
+        
+    }
+}
+
+void Measurement()
+{
+    if(reset==true){
+        Encoder.reset();
+        reset = false;
+        counter=0;
+        }
+    Roll();
+    Measure();
 }
 
 int main()
 {
     motor1DC = 1;
+    ledr = 1;
     ledb = 1;
-    ledr = 1;
     ledg = 0;
     
-    while (true) 
-    {
-        if(button1==0)
-        { 
-            Roll();
-            wait(t); // wait before next roll to protect motors
-            ledb = 1;
-            ledg = 0; // green led on = ready to roll
-        } 
-        Measure();     
-    }
+    On = false;
+    reset=true;
+    counter=0;
+    
+    tick.attach(&Measurement,F);
+    
+    while(true){}
 }
\ No newline at end of file