Servo position controlled by potvalue

Dependencies:   Servo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* This code controls the main functions of the servomotor. The servomotor needs an external power of 6 or 12 V and is connected to the PWMOut pin
00002 External power can be either 4*1.5V AA batteries (6V), 8*1.5V AA batteries (12V) or an appropiate DC voltage source. It is important that ground of both the servo motor and mBed platform are the same. 
00003 Servo motor speed can be controlled by an external signal such as a potnetiometer or in context of this project an EMG signal.
00004 */ 
00005 
00006 #include <mbed.h>
00007 #include <Servo.h> 
00008 #include <MODSERIAL.h>
00009 //#include <VarSpeedServo.h>
00010 
00011 PwmOut PWM1(D7);
00012 MODSERIAL pc(USBTX,USBRX);
00013 AnalogIn pot(A1);
00014 Ticker potmeterreadout;
00015 
00016 /* int PWMfreq(void) {
00017 PWM1.period(0.020); // set PWM period to 10 ms
00018 PWM1=0.5; // set duty cycle to 50%
00019 }
00020 */
00021 
00022 Servo myservo(D7);
00023 volatile float potvalue = 0.0;
00024 float range_servo = 0.001;
00025 float position_servo = 0.05;
00026 float servo_period = 0.01f;
00027 
00028 void readpot(){
00029     potvalue = pot.read();
00030     //position = motor1.getPosition();
00031     pc.printf("reference position = %.2f\r", potvalue);
00032     myservo.calibrate(range_servo, 55.0); 
00033     myservo = potvalue;
00034     //motorpid = PID(potvalue - position, M1_KP, M1_KI, M1_KD, M1_TS, m1_err_int, m1_prev_err, m1_f_v1, m1_f_v2, M1_F_A1, M1_F_A2, M1_F_B0, M1_F_B1, M1_F_B2);
00035     }
00036  // output PID controller is not yet tested.
00037 
00038 
00039 int main() {
00040  
00041     float range_servo = 0.0008;
00042     float position_servo = 0.5;
00043     
00044     pc.baud(9600);
00045     potmeterreadout.attach(readpot,0.01f);
00046     PWM1.period(servo_period);
00047     
00048     while(1){            
00049 
00050     }
00051 }
00052 
00053 
00054