Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "VARIABLES.h"
- #include <Wire.h>
- #include <Adafruit_PWMServoDriver.h>
- #include <Arduino.h>
- #include <Adafruit_ADS1015.h>
- #include "Adafruit_MCP23017.h"
- #include "DRV8825.h"
- Adafruit_PWMServoDriver ServoDriver = Adafruit_PWMServoDriver();
- Adafruit_MCP23017 mcp;
- Adafruit_ADS1115 ads;
- DRV8825 stepper(MOTOR_STEPS, DIR, STEP, ENABLE, MODE0, MODE1, MODE2);
- void drive_claw()
- {
- if(buttonState[2]==LOW)
- {
- if(ClawAngle-1>=0)
- {
- ClawAngle--;
- drive_servo(4,ClawAngle);
- }
- }
- if(buttonState[3]==LOW)
- {
- if(ClawAngle+1<=180)
- {
- ClawAngle++;
- drive_servo(4,ClawAngle);
- }
- }
- //Serial.print("CLAW ANGLE : ");
- //Serial.println(ClawAngle);
- }
- int drive_servo(int servo,int DesiredPos)
- {
- if(DesiredPos >=0 && DesiredPos <=180)
- {
- if(servo==0) //BASE SERVO
- {
- if(DesiredPos!=BasePos)
- {
- if(DesiredPos>BasePos)
- {
- if(DesiredPos-BasePos>20)
- {
- temp=BasePos+10;
- while(BasePos<=temp) //While in the first 10 degrees
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos++;
- delayMicroseconds(StartServoDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- temp=DesiredPos-10;
- while(BasePos <=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- temp=0;
- while(BasePos <=DesiredPos)
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos++;
- delayMicroseconds(StopServoDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- }
- else
- {
- while(BasePos <=DesiredPos)
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- }
- }
- if(DesiredPos<BasePos)
- {
- if(BasePos-DesiredPos>20)
- {
- temp=BasePos-10;
- while(BasePos>=temp) //While in the first 10 degrees
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos--;
- delayMicroseconds(StartServoDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- temp=DesiredPos+10;
- while(BasePos >=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("BasePos ");
- ////Serial.println(BasePos);
- }
- temp=0;
- while(BasePos >=DesiredPos)
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos--;
- delayMicroseconds(StopServoDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- }
- else
- {
- while(BasePos >=DesiredPos)
- {
- move_servo(0,BasePos);
- move_servo(1,BasePos);
- BasePos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("BasePos ");
- //Serial.println(BasePos);
- }
- }
- }
- }
- }
- if(servo==1) //J1 SERVO
- {
- if(DesiredPos!=J2Pos)
- {
- if(DesiredPos>J2Pos)
- {
- if(DesiredPos-J2Pos>20)
- {
- temp=J2Pos+10;
- while(J2Pos<=temp) //While in the first 10 degrees
- {
- move_servo(2,J2Pos);
- J2Pos++;
- delayMicroseconds(StartServoDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- temp=DesiredPos-10;
- while(J2Pos <=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(2,J2Pos);
- J2Pos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- temp=0;
- while(J2Pos <=DesiredPos)
- {
- move_servo(2,J2Pos);
- J2Pos++;
- delayMicroseconds(StopServoDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- }
- else
- {
- while(J2Pos <=DesiredPos)
- {
- move_servo(2,J2Pos);
- J2Pos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- }
- }
- if(DesiredPos<J2Pos)
- {
- if(J2Pos-DesiredPos>20)
- {
- temp=J2Pos-10;
- while(J2Pos>=temp) //While in the first 10 degrees
- {
- move_servo(2,J2Pos);
- J2Pos--;
- delayMicroseconds(StartServoDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- temp=DesiredPos+10;
- while(J2Pos >=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(2,J2Pos);
- J2Pos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- temp=0;
- while(J2Pos >=DesiredPos)
- {
- move_servo(2,J2Pos);
- J2Pos--;
- delayMicroseconds(StopServoDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- }
- else
- {
- while(J2Pos >=DesiredPos)
- {
- move_servo(2,J2Pos);
- J2Pos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J2Pos ");
- //Serial.println(J2Pos);
- }
- }
- }
- }
- }
- if(servo==2) //J3 SERVO
- {
- if(DesiredPos!=J3Pos)
- {
- if(DesiredPos>J3Pos)
- {
- if(DesiredPos-J3Pos>20)
- {
- temp=J3Pos+10;
- while(J3Pos<=temp) //While in the first 10 degrees
- {
- move_servo(3,J3Pos);
- J3Pos++;
- delayMicroseconds(StartServoDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- temp=DesiredPos-10;
- while(J3Pos <=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(3,J3Pos);
- J3Pos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- temp=0;
- while(J3Pos <=DesiredPos)
- {
- move_servo(3,J3Pos);
- J3Pos++;
- delayMicroseconds(StopServoDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- }
- else
- {
- while(J3Pos <=DesiredPos)
- {
- move_servo(3,J3Pos);
- J3Pos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- }
- }
- if(DesiredPos<J3Pos)
- {
- if(J3Pos-DesiredPos>20)
- {
- temp=J3Pos-10;
- while(J3Pos>=temp) //While in the first 10 degrees
- {
- move_servo(3,J3Pos);
- J3Pos--;
- delayMicroseconds(StartServoDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- temp=DesiredPos+10;
- while(J3Pos >=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(3,J3Pos);
- J3Pos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- temp=0;
- while(J3Pos >=DesiredPos)
- {
- move_servo(3,J3Pos);
- J3Pos--;
- delayMicroseconds(StopServoDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- }
- else
- {
- while(J3Pos >=DesiredPos)
- {
- move_servo(3,J3Pos);
- J3Pos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("J3Pos ");
- //Serial.println(J3Pos);
- }
- }
- }
- }
- }
- if(servo==3)
- {
- if(DesiredPos!=WristPos)
- {
- if(DesiredPos>WristPos)
- {
- if(DesiredPos-WristPos>20)
- {
- temp=WristPos+10;
- while(WristPos<=temp) //While in the first 10 degrees
- {
- move_servo(4,WristPos);
- WristPos++;
- delayMicroseconds(StartServoDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- temp=DesiredPos-10;
- while(WristPos <=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(4,WristPos);
- WristPos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- temp=0;
- while(WristPos <=DesiredPos)
- {
- move_servo(4,WristPos);
- WristPos++;
- delayMicroseconds(StopServoDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- }
- else
- {
- while(WristPos <=DesiredPos)
- {
- move_servo(4,WristPos);
- WristPos++;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- }
- }
- if(DesiredPos<WristPos)
- {
- if(WristPos-DesiredPos>20)
- {
- temp=WristPos-10;
- while(WristPos>=temp) //While in the first 10 degrees
- {
- move_servo(4,WristPos);
- WristPos--;
- delayMicroseconds(StartServoDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- temp=DesiredPos+10;
- while(WristPos >=temp) //While in the middle of the move till 10 degrees from target angle
- {
- move_servo(4,WristPos);
- WristPos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- temp=0;
- while(WristPos >=DesiredPos)
- {
- move_servo(4,WristPos);
- WristPos--;
- delayMicroseconds(StopServoDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- }
- else
- {
- while(WristPos >=DesiredPos)
- {
- move_servo(4,WristPos);
- WristPos--;
- delayMicroseconds(ServoMoveDelay);
- //Serial.print("WristPos ");
- //Serial.println(WristPos);
- }
- }
- }
- }
- }
- }
- }
- int move_servo(int servonum , int angle)
- {
- pulselen=map(angle,0,180,SERVOMIN,SERVOMAX);
- ServoDriver.setPWM(servonum, 0, pulselen);
- }
- void drive_stepper()
- {
- stepper.setMicrostep(microsteps);
- if(buttonState[0]==LOW && buttonState[4]==HIGH) //While Switch to move is pressed and limit switch is not
- {
- if(StepperPos-1 >=0)
- {
- StepperPos--;
- stepper.move(microsteps*MOTOR_STEPS/defaultStep);
- }
- }
- if(buttonState[1]==LOW&& buttonState[4]==HIGH)
- {
- if(StepperPos +1 <=360)
- {
- StepperPos++;
- stepper.move(-microsteps*MOTOR_STEPS/defaultStep);
- }
- }
- Serial.println(StepperPos);
- }
- void home_stepper()
- {
- stepper.setMicrostep(microsteps);
- while(buttonState[4]==HIGH)
- {
- stepper.move(microsteps*MOTOR_STEPS/360);
- read_controls();
- Serial.println("HOMING");
- //delay(1);
- }
- StepperPos=180;
- stepper.move(-microsteps*MOTOR_STEPS/2);
- }
- void read_controls()
- {
- adc0=ads.readADC_SingleEnded(0)/64;
- adc1=ads.readADC_SingleEnded(1)/64;
- adc2=ads.readADC_SingleEnded(2)/64;
- adc3=ads.readADC_SingleEnded(3)/64;
- X1Angle=map(adc0,0,356,0,180)-2;
- Y1Angle=map(adc1,0,384,0,180)-1;
- X2Angle=map(adc2,0,359,0,180)-1;
- Y2Angle=map(adc3,0,364,0,180)-1;
- //Serial.print(X1Angle);
- //Serial.print(" , ");
- //Serial.print(Y1Angle);
- //Serial.print(" , ");
- //Serial.print(X2Angle);
- //Serial.print(" , ");
- //Serial.println(Y2Angle);
- // Reading MCP
- for(int i=0;i<=5;i++)
- {
- buttonState[i]=mcp.digitalRead(i);
- }
- }
- void setup()
- {
- Serial.begin(9600);
- ads.begin();
- mcp.begin();
- mcp.pullUp(0,HIGH);
- mcp.pullUp(1,HIGH);
- mcp.pullUp(2,HIGH);
- mcp.pullUp(3,HIGH);
- mcp.pullUp(4,HIGH);
- mcp.pullUp(5,HIGH);
- mcp.pinMode(0, INPUT); //J1Button
- mcp.pinMode(1, INPUT); //J2Button
- mcp.pinMode(2, INPUT); //Button1
- mcp.pinMode(3, INPUT); //Button2
- mcp.pinMode(4, INPUT); // Limit Switch
- mcp.pinMode(5, INPUT); //Change defaultStep variable
- ServoDriver.begin();
- ServoDriver.setPWMFreq(60);
- stepper.begin(RPM);
- stepper.enable();
- ServoMoveDelay=300;
- for(int i=0;i<=4;i++)
- {
- drive_servo(i,90);
- }
- //DEBOUNCE CODE
- //for(int i=0;i<=4;i++)
- //{
- //lastButtonState[i]=HIGH;
- //}
- ServoMoveDelay=0;
- read_controls();
- home_stepper();
- }
- void loop()
- {
- read_controls();
- drive_servo(0,X1Angle);
- drive_servo(1,Y1Angle);
- drive_servo(2,X2Angle);
- drive_servo(3,Y2Angle);
- drive_claw();
- drive_stepper();
- }
- Variables.h
- #ifndef VARIABLES_H
- #define VARIABLES_H
- int adc0, adc1, adc2, adc3; // ADC Readings
- int X1Angle,Y1Angle,X2Angle,Y2Angle; //Joystick Angle Variables
- int SERVOMIN =155; // Servo Pulse Widths
- int SERVOMAX =538;
- int RPM=50; //Stepper Speed
- int MOTOR_STEPS=200;
- int defaultStep=32; //MOTOR_STEPS/defaultStep=200/360=1 degrees/Step MOTOR_STEPS/200=1.8 degrees/Step
- double pulselength; // Servo Variable
- int StepperPos; // Stepper
- int BasePos; //Base
- int J2Pos; // J2
- int J3Pos; //J3
- int WristPos; //Wrist
- int ClawPos; //Claw
- int ClawAngle; //The angle that gets changed using the joystick buttons
- int ServoDelay=500; // Servo Delay time In us
- int HOME=90; // HOMING ANGlE for the servos
- int StepperHome=180; // Stepper Homing Angle
- int temp; //Servo Temp Position Variable
- int ServoMoveDelay=0;
- int StartServoDelay=200;
- int StopServoDelay=300;
- int pulselen;
- int buttonState[6];
- int microsteps=16; //Stepper Microstepping Value
- #define ENABLE 7
- #define STEP 3
- #define DIR 2
- #define MODE0 6
- #define MODE1 5
- #define MODE2 4
- #endif
Advertisement
Add Comment
Please, Sign In to add comment