Servo position controlled by potvalue

Dependencies:   Servo

Committer:
MMartens
Date:
Fri Oct 20 06:58:16 2017 +0000
Revision:
3:640691434e7e
Parent:
1:34cea73289c4
Servo position control - full speed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MMartens 0:fe46ce7f03d3 1 /* 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
MMartens 0:fe46ce7f03d3 2 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.
MMartens 0:fe46ce7f03d3 3 Servo motor speed can be controlled by an external signal such as a potnetiometer or in context of this project an EMG signal.
MMartens 0:fe46ce7f03d3 4 */
MMartens 0:fe46ce7f03d3 5
MMartens 0:fe46ce7f03d3 6 #include <mbed.h>
MMartens 1:34cea73289c4 7 #include <Servo.h>
MMartens 1:34cea73289c4 8 #include <MODSERIAL.h>
MMartens 1:34cea73289c4 9 //#include <VarSpeedServo.h>
MMartens 1:34cea73289c4 10
MMartens 1:34cea73289c4 11 PwmOut PWM1(D7);
MMartens 1:34cea73289c4 12 MODSERIAL pc(USBTX,USBRX);
MMartens 1:34cea73289c4 13 AnalogIn pot(A1);
MMartens 1:34cea73289c4 14 Ticker potmeterreadout;
MMartens 1:34cea73289c4 15
MMartens 1:34cea73289c4 16 /* int PWMfreq(void) {
MMartens 1:34cea73289c4 17 PWM1.period(0.020); // set PWM period to 10 ms
MMartens 1:34cea73289c4 18 PWM1=0.5; // set duty cycle to 50%
MMartens 1:34cea73289c4 19 }
MMartens 1:34cea73289c4 20 */
MMartens 0:fe46ce7f03d3 21
MMartens 1:34cea73289c4 22 Servo myservo(D7);
MMartens 1:34cea73289c4 23 volatile float potvalue = 0.0;
MMartens 1:34cea73289c4 24 float range_servo = 0.001;
MMartens 1:34cea73289c4 25 float position_servo = 0.05;
MMartens 1:34cea73289c4 26 float servo_period = 0.01f;
MMartens 1:34cea73289c4 27
MMartens 1:34cea73289c4 28 void readpot(){
MMartens 1:34cea73289c4 29 potvalue = pot.read();
MMartens 1:34cea73289c4 30 //position = motor1.getPosition();
MMartens 1:34cea73289c4 31 pc.printf("reference position = %.2f\r", potvalue);
MMartens 1:34cea73289c4 32 myservo.calibrate(range_servo, 55.0);
MMartens 1:34cea73289c4 33 myservo = potvalue;
MMartens 1:34cea73289c4 34 //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);
MMartens 1:34cea73289c4 35 }
MMartens 1:34cea73289c4 36 // output PID controller is not yet tested.
MMartens 1:34cea73289c4 37
MMartens 1:34cea73289c4 38
MMartens 0:fe46ce7f03d3 39 int main() {
MMartens 0:fe46ce7f03d3 40
MMartens 1:34cea73289c4 41 float range_servo = 0.0008;
MMartens 1:34cea73289c4 42 float position_servo = 0.5;
MMartens 1:34cea73289c4 43
MMartens 1:34cea73289c4 44 pc.baud(9600);
MMartens 3:640691434e7e 45 potmeterreadout.attach(readpot,0.01f);
MMartens 1:34cea73289c4 46 PWM1.period(servo_period);
MMartens 0:fe46ce7f03d3 47
MMartens 1:34cea73289c4 48 while(1){
MMartens 3:640691434e7e 49
MMartens 0:fe46ce7f03d3 50 }
MMartens 0:fe46ce7f03d3 51 }
MMartens 0:fe46ce7f03d3 52
MMartens 0:fe46ce7f03d3 53
MMartens 0:fe46ce7f03d3 54