farmbot-arduino-firmware/src/ServoControl.cpp

86 lines
1.3 KiB
C++
Raw Normal View History

2014-11-04 15:14:35 -07:00
#include "ServoControl.h"
2015-06-14 15:08:57 -06:00
#include "TimerOne.h"
#include <Servo.h>
2014-11-04 15:14:35 -07:00
/*
Servo pin layout
D11 D6 D5 D4
*/
2017-04-19 08:12:12 -06:00
static ServoControl *instance;
2014-11-04 15:14:35 -07:00
2019-02-08 14:17:43 -07:00
Servo servos[4];
2014-11-04 15:14:35 -07:00
2017-04-19 08:12:12 -06:00
ServoControl *ServoControl::getInstance()
{
if (!instance)
{
instance = new ServoControl();
};
return instance;
};
2014-11-04 15:14:35 -07:00
2017-04-19 08:12:12 -06:00
ServoControl::ServoControl()
{
2014-11-04 15:14:35 -07:00
}
2017-04-19 08:12:12 -06:00
int ServoControl::attach()
{
servos[0].attach(SERVO_0_PIN);
servos[1].attach(SERVO_1_PIN);
2019-02-08 14:17:43 -07:00
servos[2].attach(SERVO_2_PIN);
servos[3].attach(SERVO_3_PIN);
2019-02-07 16:22:36 -07:00
}
void ServoControl::detachServos()
{
servos[0].detach();
servos[1].detach();
2019-02-08 14:17:43 -07:00
servos[2].detach();
servos[3].detach();
2014-11-04 15:14:35 -07:00
}
2017-04-19 08:12:12 -06:00
int ServoControl::setAngle(int pin, int angle)
{
2014-11-05 14:54:49 -07:00
2017-04-19 08:12:12 -06:00
/*
2014-11-05 14:54:49 -07:00
Serial.print("R99");
Serial.print(" ");
Serial.print("SERVO");
Serial.print(" ");
Serial.print("pin");
Serial.print(" ");
Serial.print(pin);
Serial.print(" ");
Serial.print("angle");
Serial.print(" ");
Serial.print(angle);
Serial.print(" ");
Serial.print("\r\n");
2014-11-05 14:54:49 -07:00
*/
2014-11-04 15:14:35 -07:00
2017-04-19 08:12:12 -06:00
switch (pin)
{
case 4:
2019-02-07 16:22:36 -07:00
servos[0].attach(SERVO_0_PIN);
2017-04-19 08:12:12 -06:00
servos[0].write(angle);
break;
case 5:
2019-02-07 16:22:36 -07:00
servos[1].attach(SERVO_1_PIN);
2017-04-19 08:12:12 -06:00
servos[1].write(angle);
break;
2019-02-07 16:22:36 -07:00
case 6:
2019-02-08 14:17:43 -07:00
servos[2].attach(SERVO_2_PIN);
servos[2].write(angle);
break;
2019-02-07 16:22:36 -07:00
case 11:
2019-02-08 14:17:43 -07:00
servos[3].attach(SERVO_3_PIN);
servos[3].write(angle);
break;
2017-04-19 08:12:12 -06:00
default:
return 1;
}
2014-11-04 15:14:35 -07:00
2017-04-19 08:12:12 -06:00
return 0;
}