Hello everyone, Im looking for some help with my project which uses the code below to control a large DC motor as a servo with hobby radio control. I have a potentiometer attached to the drive shaft for position feedback.
arduino sends the pwm signals to the motor controller which then turns the motor. i followed this tutorial on youtube to build my "rc servo" and it works well. How to Make an R/C Servo from a Wiper Motor | James Bruton - YouTube
I am currenty using the motor controller IBT2 BTS7960 it requires 2x pwm pins, 1 is forward and the other is reverse. the enable pins are permently connected to 5v.
I would like to change over to another type of controller like a motomonster which uses only 1 pwm pin for both forward and backward (after enabling either the A or B direction pin.)
does anyone know if this would be possible ? any help to solve this is greatly appreciated and thank you for looking.
[code]
#include <PID_v1.h> //PID loop from http://playground.arduino.cc/Code/PIDLibrary
double Pk1 = 2; //speed it gets there
double Ik1 = 0;
double Dk1 = 0;
double Setpoint1, Input1, Output1, Output1a; // PID variables
PID PID1(&Input1, &Output1, &Setpoint1, Pk1, Ik1 , Dk1, DIRECT); // PID Setup
volatile unsigned long pwm;
volatile boolean done;
unsigned long start;
int pot;
unsigned long currentMillis;
long previousMillis = 0; // set up timers
long interval = 20; // time constant for timers
void setup() {
pinMode(2, INPUT);
pinMode(A0, INPUT);
pinMode(5,OUTPUT);
pinMode(6,OUTPUT);
attachInterrupt(0, timeit, CHANGE);
Serial.begin(115200);
PID1.SetMode(AUTOMATIC); // PID Setup - trousers SERVO
PID1.SetOutputLimits(-255, 255);
PID1.SetSampleTime(20);
}
void timeit() {
if (digitalRead(2) == HIGH) {
start = micros();
}
else {
pwm = micros() - start;
done = true;
}
}
void loop() {
currentMillis = millis();
if (currentMillis - previousMillis >= interval) { //start timed event
previousMillis = currentMillis;
pot = analogRead(A0);
Serial.print(pot);
Serial.print(" , ");
Serial.print (pwm);
Serial.print(" , ");
Setpoint1 = map(pwm,1000,2000,-255,255);
Input1 = map(pot,0,1023,-255,255);
PID1.Compute();
Serial.println(Output1);
if (Output1 > 0) {
analogWrite(5, Output1);
analogWrite(6, 0);
}
else if (Output1 < 0) {
Output1a = abs(Output1);
analogWrite(5, 0);
analogWrite(6, Output1a);
}
if (!done)
return;
done = false;
} // end of timed event
}
[/code]