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

Dependencies:   Servo Motor

main.cpp

Committer:
m215676
Date:
2018-10-22
Revision:
3:65416d9052dd
Parent:
0:047ab874a574
Child:
4:5ba96337741c

File content as of revision 3:65416d9052dd:

/*
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(p23); //servo to move monument on turn table
Servo angle(p22); //servo to adjust horizontal angle of arm
DigitalIn herndon_sw1(p17); //moves herndon monument on turn table continuously while switch is on
AnalogIn turn(p16);  //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()) {
            printf("Herdnon is at %f\n", x);
            
            x = x + xinc;
            if (x > 1.0) {
                x = 1.0;
                xinc = -0.1;
                
                printf("Herdnon is at %f\n", x);
                
            } // 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);
            
            printf("Herdnon is at %f\n", x);
            
        } // s1 if off

        ThisThread::sleep_for(200); // change this number if too fast or too slow
    } // while(1)

}// herndon_callback()