Fa2018-es200-1121-project2-herndon-toss / Mbed OS herndon-threaded

Dependencies:   Servo Motor

Revision:
0:047ab874a574
Child:
3:65416d9052dd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 22 12:22:52 2018 +0000
@@ -0,0 +1,143 @@
+/*
+ES200 1121 Project 2 Herndon Cover Toss - threaded version
+Get threaded!
+*/
+
+#include "mbed.h"
+#include "rtos.h"
+
+#include "stdio.h"
+#include "Motor.h"
+#include "Servo.h"
+
+
+// Connections to hardware defined here
+Serial pc(USBTX,USBRX); // connection to laptop for debugging
+Motor m(p26, p30, p29); //launches the cover
+Servo herndon_servo(p21); //servo to move monument on turn table
+Servo angle(p22); //servo to adjust horizontal angle of arm
+DigitalIn herndon_sw1(p25); //moves herndon monument on turn table continuously while switch is on
+AnalogIn turn(p15);  //turn dial to adjust servo for horizontal angle of arm SWITCH 2
+DigitalIn swtch3(p18); //releases the cover when switch 3 is turned on
+DigitalIn swtch4(p19); //retracts arm back to starting position to try again
+
+//DigitalIn sw1 (p16) ;     not sure why this is here - maybe broken pin
+
+// other variables
+int s1, s3, s4, i;
+float x;
+float y;
+
+
+// Herndon wiggling thread stuff
+Thread herndon_thread;  // thread to run Herndon in
+void herndon_callback(void);  // function for driving Herndon
+
+
+
+/*
+One thread: 
+herndon sw Switch 1 - turns on and makes the herndon monument move continously
+
+Analog in turn - for aiming
+Switch 3 - release cover
+Switch 4 - back to battery (only after relase) 
+*/
+
+
+int main(void)
+{
+    // setup stuff
+    pc.printf("ES200 1121 Project 2 Group 3\r\n");
+    pc.printf("Herndon Cover Toss Game\r\n");
+    pc.printf("main() thread running\r\n");
+    herndon_servo.calibrate(0.0009, 90.0); // HiTec HS422 servo ES200/202 numbers
+    angle.calibrate(0.0009, 90.0); // HiTec HS422 servo ES200/202 numbers
+    m.speed(0.0);
+
+    // start the Herndon thread
+    herndon_thread.start(callback(herndon_callback));
+    pc.printf("herndon_thread running\r\n");
+
+
+
+
+        // main loop for user interaction goes here
+    } // main()
+
+
+    
+    /*
+    //Adjust horiztonal angle with switch 2/dial    
+       while(1) {
+    y = turn.read(); //reads the state of the dial the user changes
+        printf("The dial is at %f\n", x);
+    angle.write(y);  //makes the servo turn according to the dial
+        printf("The servo is at %f\n", x);
+   } //end of while loop
+
+
+    
+    swtch3.read(); //read state of switch 3 (launch the cover)
+    if (swtch3==1) {
+        while(1) {
+            printf("motor on");
+            m.speed(1.0);
+            wait (.4);
+            printf("motor off");
+            m.speed(0.0);
+            wait (1);
+            break;
+        } //end of while
+        } //end of if
+        
+        
+        
+        
+    s4 = swtch4.read(); //read state of switch 4 (retract arm to try again)
+    if (s4 == 1){
+        while(1) {
+           printf("reversed");
+            m.speed(-.25);
+            wait(.6);
+            printf("motor off");
+            m.speed(0);
+            wait(1);
+            break;
+        } //end of while
+        } //end of if
+        }//end of int main
+    */
+
+
+
+
+/** Callback for running Herndon wiggling thread as its own self-contained thing
+*/
+void herndon_callback(void)
+{
+    float x=0.0; // current position of Herndon
+    float xinc=0.1; // increment to move Herndon each time step
+
+    while(1) {
+        if (herndon_sw1.read()) {
+            x = x + xinc;
+            if (x > 1.0) {
+                x = 1.0;
+                xinc = -0.1;
+            } // hit the right end
+            else if (x < 0.0) {
+                x = 0.0;
+                xinc = 0.1;
+            } // hit the left end
+            herndon_servo.write(x);
+        } // s1 is on
+
+        else {
+            herndon_servo.write(0.0);
+        } // s1 if off
+
+        ThisThread::sleep_for(200); // change this number if too fast or too slow
+    } // while(1)
+
+}// herndon_callback()
\ No newline at end of file