Servo position controlled by potvalue

Dependencies:   Servo

main.cpp

Committer:
MMartens
Date:
2017-10-12
Revision:
1:34cea73289c4
Parent:
0:fe46ce7f03d3
Child:
3:640691434e7e

File content as of revision 1:34cea73289c4:

/* 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
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. 
Servo motor speed can be controlled by an external signal such as a potnetiometer or in context of this project an EMG signal.
*/ 

#include <mbed.h>
#include <Servo.h> 
#include <MODSERIAL.h>
//#include <VarSpeedServo.h>

PwmOut PWM1(D7);
MODSERIAL pc(USBTX,USBRX);
AnalogIn pot(A1);
Ticker potmeterreadout;

/* int PWMfreq(void) {
PWM1.period(0.020); // set PWM period to 10 ms
PWM1=0.5; // set duty cycle to 50%
}
*/

Servo myservo(D7);
volatile float potvalue = 0.0;
float range_servo = 0.001;
float position_servo = 0.05;
float servo_period = 0.01f;

void readpot(){
    potvalue = pot.read();
    //position = motor1.getPosition();
    pc.printf("reference position = %.2f\r", potvalue);
    myservo.calibrate(range_servo, 55.0); 
    myservo = potvalue;
    //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);
    }
 // output PID controller is not yet tested.


int main() {
    printf("Servo Calibration Controls:\n");
    printf("1,2,3 - Position Servo (full left, middle, full right)\n");
    printf("4,5 - Decrease or Increase range\n");
 
    float range_servo = 0.0008;
    float position_servo = 0.5;
    
    pc.baud(9600);
    potmeterreadout.attach(readpot,0.05f);
    PWM1.period(servo_period);
    
    while(1){            
        // Safety features to override potvalue       
        /*switch(pc.getc()) {
            case '1': position_servo = 0.0; break;
            case '2': position_servo = 0.5; break;
            case '3': position_servo = 1.0; break;
            case '4': range_servo += 0.0001; break; 
            case '5': range_servo -= 0.0001; break; 
        }
        */
        //printf("position = %.1f, range = +/-%0.4f\r", position_servo, range_servo);
        //myservo.calibrate(range_servo, 110.0); 
        //myservo = position_servo;
    }
}

/* Example code to control the servomotor by a potentiometer, similarly one could control the speed of the servomotor. 
The speed can be controlled by using the library VarSpeedServo.h 
By mapping the intensity of EMG signal to a range of values (e.g. 0:1:100) you can easily control the speed and position of the motor
*/

/*
 Servo servo_test;      //initialize a servo object for the connected servo  
                
 int angle = 0;    
 int potentio = A0;      // initialize the A0analog pin for potentiometer

 
 void setup() 
 { 
  servo_test.attach(9);     // attach the signal pin of servo to pin9 of arduino
 } 
 
 void loop() 
 { 
  angle = analogRead(potentio);            // reading the potentiometer value between 0 and 1023 
  angle = map(angle, 0, 1023, 0, 179);     // scaling the potentiometer value to angle value for servo between 0 and 180) 
  servo_test.write(angle);                   //command to rotate the servo to the specified angle 
  delay(5);             
 }  
 */