Friday, 22 September 2017

Resolving Issues with Forward and Reverse Runs

In the previous post, I had mentioned how the shift from forward to reverse and vice versa was quite unpredictable with an undocumented ESC. Using a series of empirically determined instructions, I had managed to make the motor run both in forward and reverse - but strictly according to a predefined series of instructions. Whenever we changed the series of instructions, the shift from forward to reverse [or vice versa] would not happen.

Here, I present an improvement upon that code: one that takes a Serial Input - and then acts on it. In my rover, I was able to make the motor run either in forward or in reverse according to the input. The caveat is that between every shift in direction, I had added a brake instruction. I still need to check if the brake instruction can be done away with [so far, am not brave enough to do that; but eventually].

Once the code below has been compiled and uploaded, I find that using the Serial input values 2 [forward] and 4 [reverse] interspersed with 3 [brake], works reproducibly.

[Code]
/* Arduino control of standard RC car ESC. 
Has code for both forward and reverse movements. 
The code presupposes that the ESC is already armed. Given sparse documentation for the ESC, I am not examining weird behavior changes in this ESC. However, if your ESC refuses to arm, then I would suggest the code shown in post_1 be used. 

Briefly, arming the ESC involves starting at the lowest pulse rate [typically around 700 microseconds] and going up to the max [typically 2100 microseconds]; since Arduino Servo library treats ESC as servos, one can also pulse the ESC from angles 0 [corresponding to minimum pulse] to 180 [corresponding to maximum pulse]. Each pulse must be at least 20milliseconds, though I prefer 40milliseconds just to be sure. Probably better ESCs will require shorter durations at each pulse rate.

Now that all the disclaimers are done, here goes nothing.
*/


#include<Servo.h>
Servo esc; // Call the ESC "esc"
void setup()
{
  esc.attach(9);
//Again - this is useful only during the testing part. For autonomous running, it is pointless.
  int i=0;
  Serial.begin(9600);
  delay(3000);

}

void loop()
{
  int i=0;
  char input;
  // Start at Servo Degree 70 and go till just above neutral. 
  // Note, Neutral is at 94 for this ESC.
  // This will run it in one direction.
  // Name this Step 1
  input=Serial.read();
if(input == '1')
{
  for(i=70;i<95;i++)
  {
    esc.write(i);
    delay(200);
  }
}

//Run for 2 seconds at moderately high speed (70).
// Name this Step 2
if(input =='2')
{
  esc.write(70); delay(200);
}
// Apply Brakes for 40 millisecs.
// Name this Step 3

if(input == '3')
{
  esc.write(100); delay(40);

// Apply throttle in opposite direction for 2 seconds.
// Name this Step 4
if(input == '4')
{
  esc.write(110); delay(2000);
}
// Bring down throttle to neutral.
// Name this Step 5
if(input == '5')
{
  for(i=110;i>90;i--)
  {
    esc.write(i);
    delay(200);
  }
}
// Apply brakes again - to change directions for 40 millisecs.
// Name this Step 6
if(input == '6')
{
  esc.write(70); delay(40);
}
input=NULL;
}
[/Code]