Tracking Mbed Servo

Dependencies:   C12832 Servo mbed-rtos mbed

main.cpp

Committer:
Qoramas
Date:
2015-02-26
Revision:
0:89523986a177
Child:
1:a22303e03da3

File content as of revision 0:89523986a177:

#include "mbed.h"
#include "C12832.h"
#include "Servo.h"
#include "rtos.h"

C12832 lcd(p5, p7, p6, p8, p11);
Serial pc(USBTX, USBRX);
AnalogIn aIn(p17);

float userX, userY;
float servoOutX, servoOutY;
float sensorIn;

Servo servoPan(p21);
Servo servoTilt(p22);

static float clamp(float value, float min, float max){
    return (value < min)? min : (value > max)? max : value;    
}

void getUserPosition(void const *name){
    while(1){
        pc.scanf("%f,%f", &userX, &userY);
    }
}

void getSensorValue(void const *name){
    while(1){
        sensorIn = aIn.read();
        Thread::wait(50);
    }
}

void logic(void const *name){
    while(1){
        
        servoOutX = clamp(userX, 0, 1);
        servoOutY = clamp(userY, 0, 1);
        
        //Displays debug data
        lcd.cls();
        lcd.locate(0,0);
        lcd.printf("InX: %f, OutX: %f", userX, servoOutX);
        lcd.locate(0,15);
        lcd.printf("InY: %f, OutY: %f", userY, servoOutY);
        
        Thread::wait(50);
        
    }
}

//Sets the servos to the given data
void setServoValue(void const *name){
    while(1){
        
        //Set the servos to the set values
        servoPan = servoOutX;
        servoTilt = servoOutY;
        
        Thread::wait(50);
    }
}
 
int main(){
    //Start each of the threads
    Thread t1(getUserPosition, (void *)"Th 1");
    Thread t2(getSensorValue, (void *)"Th 2");
    Thread t3(logic, (void *)"Th 3");
    Thread t4(setServoValue, (void *) "Th 4");
    
    //Stop the main process from dying
    while(1)wait(10000);
}