![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
control for robotic arm that can play chess using a granular gripper
Dependencies: Encoder mbed HIDScope Servo MODSERIAL
Fork of chessRobot by
actuators.cpp
- Committer:
- annesteenbeek
- Date:
- 2015-10-05
- Revision:
- 13:4837b36b9a68
- Parent:
- 12:61759f94c07a
- Child:
- 14:0c0d1bfd94ea
File content as of revision 13:4837b36b9a68:
#include "actuators.h" #include "PID.h" #include "mbed.h" #include "config.h" #include "encoder.h" // functions for controlling the motors bool motorEnable = false; bool direction1 = false; // CCW is false(positive rotation), CW is true (neg rotation) bool direction2 = false; double motor1Pos = 0; double motor2Pos = 0; double motorSpeed1 = 0; double motorSpeed2 = 0; double motorSetSpeed1 = 0; double motorSetSpeed2 = 0; double motorPWM1 = 0; double motorPWM2 = 0; // Set PID values double Kp1 = 1; double Ki1 = 1; double Kd1 = 1; double Kp2 = 1; double Ki2 = 1; double Kd2 = 1; void motorInit(){ // Initialze motors PwmOut motor1(motor1PWMPin); PwmOut motor2(motor2PWMPin); // Set motor direction pins. DigitalOut motor1Dir(motor1DirPin); DigitalOut motor2Dir(motor2DirPin); // Set initial direction motor1Dir.write(direction1); motor2Dir.write(direction2); // Set motor PWM period motor1.period(1/pwm_frequency); motor2.period(1/pwm_frequency); // Initialize encoders (with speed calculation) Encoder encoder1(enc1A, enc1B, true); Encoder encoder2(enc2A, enc2B, true); initPID(); } void initPID(){ // create PID instances for motors // PID pidname(input, output, setpoint, kp, ki, kd, direction) PID PIDmotor1(&motorSpeed1, &motorPWM1, &motorSetSpeed1, Kp1, Ki1, Kd1, DIRECT); PID PIDmotor2(&motorSpeed2, &motorPWM2, &motorSetSpeed2, Kp2, Ki2, Kd2, DIRECT); // set PID mode PIDmotor1.SetMode(AUTOMATIC); PIDmotor2.SetMode(AUTOMATIC); // set limits for PID output to avoid integrator build up. PIDmotor1.SetOutputLimits(-1.0, 1.0); PIDmotor2.SetOutputLimits(-1.0, 1.0); } void motorControl(){ if(motorEnable){ // only run motors if switch is enabled // get encoder positions motor1Pos = encoder1.getPosition(); motor2Pos = encoder2.getPosition(); // check if motor's are within rotational boundarys // get encoder speeds motorSpeed1 = encoder1.getSpeed(); motorSpeed2 = encoder2.getSpeed(); // translate to x/y speed // compute new PID parameters using setpoint speeds and x/y speeds PIDmotor1.compute(); PIDmotor2.compute(); // translate to motor rotation speed // write new values to motor's if (motorPWM1 > 0 ){ // CCW rotation (unitcircle convetion) direction1 = false; }else{ direction1 = true; // CW rotation } if (motorPWM2 > 0 ){ // CCW rotation (unitcircle convetion) direction2 = false; }else{ direction2 = true; // CW rotation } motor1.write(abs(motorPWM1)); motor2.write(abs(motorPWM2)); }else{ // write 0 to motors motor1.write(0); motor2.write(0); } } void servoControl(){ // use potMeter Value to set servo angle // (optionaly calculate xy position to keep balloon in position) // calculate z position using angle // calculate x y translation of endpoint // find new x and y speed. }