Looking for some assistance with getting my project up and running, a multi turn servo.
I have code to set initial position and run it manually, but need help in getting it to do exactly what i want.
Any assistance appreciated!
Looking for some assistance with getting my project up and running, a multi turn servo.
I have code to set initial position and run it manually, but need help in getting it to do exactly what i want.
Any assistance appreciated!
Code? What code?
Servo? What servo?
Well, you offer absolutely no information, but I used the Brett Beauregard PID library for a project recently and it worked quite well.
I have tried to use the Brett Beauregard PID library but i only get #include <PID_v1.h> into the sketch.
There is no other code showing.
Mega 2560, driving IBT2 H bridge. 400CPR encoder.
Here is my code so far.
#define ENCODER0PINA 20 // this pin needs to support interrupts
#define ENCODER0PINB 18 // interuppt
#define CPR 400 // encoder cycles per revolution
#define CLOCKWISE 1 // direction constant
#define COUNTER_CLOCKWISE 2 // direction constant
#define HOME_SWITCH 36 // Home limit switch
#define MOTOR_REPOSITION_TIME 2000
#define E_SWITCH 40
volatile byte state = LOW;
volatile long encoder0Position = 0; // variables modified by interrupt handler must be declared as volatile
volatile long interruptsReceived = 0;
short currentDirection = CLOCKWISE;// track direction: 0 = counter-clockwise; 1 = clockwise
long previousPosition = 0;// track last position to see if worth printing new output
int SENSOR_PIN = 0; // center pin of the potentiometer
int RPWM_Output = 5; // Arduino PWM output pin 5; connect to IBT-2 pin 1 (RPWM)
int LPWM_Output = 6; // Arduino PWM output pin 6; connect to IBT-2 pin 2 (LPWM)
void setup()
{
Serial.begin(9600);
Serial.println("<Arduino is ready>");
pinMode(RPWM_Output, OUTPUT);
pinMode(LPWM_Output, OUTPUT);
pinMode(ENCODER0PINA, INPUT_PULLUP);
pinMode(ENCODER0PINB, INPUT_PULLUP);
pinMode(HOME_SWITCH, INPUT_PULLUP); // home switch
pinMode(E_SWITCH, INPUT_PULLUP); //E stop switch, to be set.
Serial.begin (9600); // enable diagnostic output
Serial.println("\n\n\n");
Serial.println("Ready.");
analogWrite(RPWM_Output, 0); // ensure that the RPWM pin is set to 0
analogWrite(LPWM_Output, 100); // start the motor moving the device slowly towards the home position
while (digitalRead(HOME_SWITCH)) // Continually read the home switch. While the switch is HIGH
; // Do nothing.
analogWrite(LPWM_Output, 0); // The home switch has been hit! Stop the motor.
Serial.println ("Switch closed."); // Print a status message, indicating the home position has been reached.
analogWrite(RPWM_Output, 100); // start the motor moving the device slowly away from the home position
delay(MOTOR_REPOSITION_TIME); // do nothing while the motor repositions the device
analogWrite(RPWM_Output, 0); // stop the motor.
attachInterrupt(digitalPinToInterrupt(ENCODER0PINA), onInterrupt, RISING);
}
void loop()
{
noInterrupts(); // Temporarily turn off interrupts
long cur_position = encoder0Position; // take a copy of encoder0Position
interrupts(); // Turn on interrupts again.
if (cur_position != previousPosition) // Has the position changed?
{
Serial.print(cur_position, DEC);
Serial.print("\t");
Serial.print(currentDirection == CLOCKWISE ? "clockwise" : "counter-clockwise");
Serial.print("\t");
Serial.println(interruptsReceived, DEC);
previousPosition = cur_position;
}
int sensorVal = digitalRead(36); //read the pushbutton value into a variable
Serial.println(sensorVal); //print out the value of the pushbutton
int sensorValue = analogRead(SENSOR_PIN); // sensor value is in the range 0 to 1023
// the lower half of it we use for reverse rotation; the upper half for forward rotation
if (sensorValue < 512)
{
int reversePWM = -(sensorValue - 511) / 2; // reverse rotation
analogWrite(LPWM_Output, 0);
analogWrite(RPWM_Output, reversePWM);
}
else
{
int forwardPWM = (sensorValue - 512) / 2; // forward rotation
analogWrite(LPWM_Output, forwardPWM);
analogWrite(RPWM_Output, 0);
}
}
void onInterrupt() // interrupt function needs to do as little as possible
{
int a = digitalRead(ENCODER0PINA);
int b = digitalRead(ENCODER0PINB); // read both inputs
if (a == b) // b is leading a (counter-clockwise)
{
encoder0Position--;
currentDirection = COUNTER_CLOCKWISE;
}
else
{
encoder0Position++; // a is leading b (clockwise)
currentDirection = CLOCKWISE;
}
encoder0Position = encoder0Position % CPR; // track 0 to 20000
interruptsReceived++; // track the number of interrupts
}