Hello I'm new to programing and robotics. I have a project where I want to control a robotic "arm" this includes a dc motor to move a toothed belt which transports a gripper to different positions. The gripper is controlled via a micro servo.
I'm trying to use a class to initialize and control multiple robotic "arms". In the following code the dc motor moving the toothed belt works as intended but the servo does unexpected small and larger movements like wiggling or constant fast spinning. I have tried the code to control the servo (for loops to open and close the gripper) in a different sketch, and there it works fine as such I believe that the servo is not broken as I have tried my code with 2 separate servos.
Does someone know, where I have made a mistake?
#include <Adafruit_MotorShield.h>
#include <Servo.h>
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
class Greif_system {
private:
Servo servo;
Adafruit_DCMotor *dc_Motor;
int position;
int pos;
public:
//constructor
Greif_system(int motor_pin , int servo_pin, Adafruit_MotorShield &motorShield){
servo.attach(servo_pin);
dc_Motor = motorShield.getMotor(motor_pin);
position = 1;
pos = 0;
}
/////////////
//Move arm to different positions and always check, that the position starting from is correct
/////////////
void position_1_to_position_2(){
if (position != 1){
if (position == 3){
position_3_to_position_2();
}
else{
return;
}
}
dc_Motor->setSpeed(100);
dc_Motor->run(BACKWARD);
delay(6000);
dc_Motor->setSpeed(0);
position = 2;
}
void position_2_to_position_3(){
if (position != 2){
if (position == 1){
position_1_to_position_3();
}
else{
return;
}
}
dc_Motor->setSpeed(100);
dc_Motor->run(BACKWARD);
delay(6000);
dc_Motor->setSpeed(0);
position = 3;
}
void position_1_to_position_3(){
if (position != 1){
if (position == 2){
position_2_to_position_3();
}
else{
return;
}
}
position_1_to_position_2();
position_2_to_position_3();
position = 3;
}
void position_3_to_position_2(){
if (position != 3){
if (position == 1){
position_1_to_position_2();
}
else{
return;
}
}
dc_Motor->setSpeed(100);
dc_Motor->run(FORWARD);
delay(6000);
dc_Motor->setSpeed(0);
position = 2;
}
void position_2_to_position_1(){
if (position != 2){
if (position == 3){
position_3_to_position_2();
position_2_to_position_1();
}
else{
return;
}
}
dc_Motor->setSpeed(100);
dc_Motor->run(FORWARD);
delay(6000);
dc_Motor->setSpeed(0);
position = 1;
}
void position_3_to_position_1(){
if (position != 3){
if (position == 2){
position_2_to_position_1();
}
else{
return;
}
}
position_3_to_position_2();
position_2_to_position_1();
position = 1;
}
///////////////////////
//Gripper
//////////////////////
//close gripper
void greifer_zu(){
for (pos = 0; pos <= 75; pos++){
servo.write(pos);
delay(10);
}
}
//open gripper
void greifer_auf(){
for (pos = 75; pos >= 0; pos--){
servo.write(pos);
delay(10);
}
}
//neutral position for gripper 1
void greifer_1_neutral(){
servo.write(0);
}
//neutral position for gripper 2
void greifer_2_neutral(){
servo.write(50);
}
};
//Initialise Objects
Greif_system Arm_rechts(4, 8, AFMS);
Greif_system Arm_links(3, 10, AFMS);
void setup() {
AFMS.begin();
Arm_rechts.greifer_1_neutral(); //Set starting position of the servo
delay(5000);
}
void loop() {
Arm_rechts.position_1_to_position_2();
delay(1000);
Arm_rechts.greifer_zu(); //Close gripper
delay(1000);
Arm_rechts.position_2_to_position_1();
delay(3000);
Arm_rechts.position_1_to_position_3();
delay(1000);
Arm_rechts.greifer_auf(); //Open gripper
delay(5000);
Arm_rechts.position_3_to_position_1();
delay(1000);
}
