Go Down

Topic: Fighting robot lifter arm - arduino pro mini, hobbyking esc and limit switches (Read 866 times) previous topic - next topic

danjr1

Hi all

I'm trying to wire up a Hobbyking ESC with brushless motor to an Arduino Pro Mini 5v with limit switches to make a lifter arm but I'm having some issues. I've double checked the ESC and rc receiver and that's all working fine but as soon as I connect the ESC's BEC to the arduino and limit switches, it doesn't work. I've also connected the negative sides of the limit switches to two pins on the arduino for the signal to tell the ESC when to stop, and the power to the switches are wired in parallel direct from the arduino/ ESC.

Any help would be greatly appreciated!!

Cheers

Dan

slipstick

What motor and what ESC? If you're trying to make the motor do less than a single rotation that's not going to work with a standard sensorless motor.

Steve

danjr1

I'm trying to emulate what a fellow roboteer has done previously where the arduino acts as a filter, stopping the ESC/ motor in one direction once a switch has depressed and only allowing the motor to rotate the other way until again it hits a limit switch (it will be many rotations as its going through a winch gearbox).

I've attached a quick sketch of the wiring i tried, along with the code. Details of the kit im using are:

*  Hobbyking F80a ESC with 5v/ 4A BEC
*  Arduino Pro Mini 5v
*  OrangeRx R620X rc receiver
*  Standard push to break spring buttons

Code:

Code: [Select]

#include <Servo.h>  //Servo library to control your ESC.
Servo esc;  //Declare the servo name as 'esc'.
 
int ch1; //PPM input from the Rx (RC receiver) goes here.
int throttle = 0; //Serial input is mapped onto this value, should be within the range 0 to 179.
int count = 0;  //Variable to delay activation until ESC has bound.
int lowerSwitchState = 0; //Variable to track the status of the lower limit switch.
int upperSwitchState = 0; //Variable to track the status of the upper limit switch.
 
void setup() {
 
  pinMode(10, INPUT); //Input from Rx
  pinMode(2, INPUT);  //Lower switch (grey wire)
  pinMode(3, INPUT);  //Upper switch (green wire)
  Serial.begin(9600); //Start the serial output (for debugging purposes).
  esc.attach(11);  //Bind the ESC to the arduino, just as it would to a receiver.
}
 
void loop() {
  lowerSwitchState = digitalRead(2); //Read pin 2 (the lower limit switch).
  upperSwitchState = digitalRead(3); //Read pin 3 (the upper limit switch).
  ch1 = pulseIn(5, HIGH, 25000);  //Read the PPM input from the Rx data line.
  if(ch1 > 1056){ /*Reject any PPM values that are outside the accepted range.
    I found the motor tends to jitter horribly if I don't have this line, as for some reason,
    0 is sent through every other time it reads.*/
  throttle = map(ch1, 1069, 1900, 80, 100); /*Map the values between 1069 and 1900
  (observed upper and lower limits of the throttle signal from the Rx) to the
  upper and lower limits of the ESC signal. These can be in the range 0-179, where
  90 is the middle position (0 movement if your ESC is reversible).
  Here it's only mapped from 80-100 in order to slow the motor down.
  You can adjust this to suit your motor or application.*/
  //NOTE: If your ESC only supports one-way movement, you'll need to
  //change any '90's in this program to '0' (for no movement) and adjust other values accordingly.
  }
  if(count < 500){
    esc.write(90);  //Essentially wait for 500 loops to ensure the ESC has bound.
    count ++;
  }else if(lowerSwitchState == LOW && throttle < 90){
    esc.write(90);  //If the signal is trying to push the actuator
    //past its lower limit, set the output to 90 for no movement.
  }else if(upperSwitchState == LOW && throttle > 90){
    esc.write(90);  //Same as above, but with upper limit.
  }else{
  esc.write(throttle);  //If all is well, relay the input Rx signal
  //to the ESC.
  }
  //NOTE: If your motor still tries to push past the limits, remember
  //to check that your motor (and actuator) direction is congruent with the
  //numbers here (0 for full reverse, 179 for full forwards).
  //If it isn't, you can switch two of your three motor leads around, or
  //swap the '<' and '>' in the above two 'if' statements.
}

slipstick

The 500 loops thing is a bit odd. Most people just put a short delay() after the attach() in setup().

Also your switch wiring sounds strange. A "standard push to break button" only uses two pins. You usually only use +5V and ground if it's a changeover switch not a push to break (normally closed) switch. So exactly what are the limit switches you are using and how are they wired?

Anyway if "doesn't work" means the motor doesn't do anything at all then two useful things you could do:
1) write a very simple sketch which just writes 80 then 100 to the motor to check that it will actually run with those values (I've seen ESCs with a dead band greater than that).
2)put some debug Serial.prints in to see where the code is getting to and look particularly at the state of the switches

Steve

danjr1

Apologies, my drawing wasn't very clear. I've wired the switches in parallel to the power pads (Raw and ground) on the arduino which is where the power from the ESC's BEC is also connected. From where the parallel negative connections are made on the switches, I've also soldered on wires which then connect to pins 2 and 3 on the board to provide the feedback.

The switches I'm using are the ones linked below:

https://uk.rs-online.com/web/p/push-button-switches/1336502/


danjr1

rewrote the code a little to include arming the ESC and mapping the throttle a little better:

Code: [Select]

#include <Servo.h>  //Servo library to control your ESC.
Servo esc;  //Declare the servo name as 'esc'.
 
int ch1; //PPM input from the Rx (RC receiver) goes here.
int throttle = 0; //Serial input is mapped onto this value, should be within the range 0 to 179.
int count = 0;  //Variable to delay activation until ESC has bound.
int lowerSwitchState = 0; //Variable to track the status of the lower limit switch.
int upperSwitchState = 0; //Variable to track the status of the upper limit switch.
 
void setup() {
 
  pinMode(10, INPUT); //Input from Rx
  pinMode(2, INPUT);  //Lower switch (grey wire)
  pinMode(3, INPUT);  //Upper switch (green wire)
  Serial.begin(9600); //Start the serial output (for debugging purposes).
  esc.attach(11);  //Bind the ESC to the arduino, just as it would to a receiver.

  esc.write(0);
  delay(100);
  esc.write(179);
  delay(100);
  esc.write(0);
  delay(100);
  esc.write(90);
}
 
void loop() {
  lowerSwitchState = digitalRead(2); //Read pin 2 (the lower limit switch).
  upperSwitchState = digitalRead(3); //Read pin 3 (the upper limit switch).
  ch1 = pulseIn(10, HIGH, 25000);  //Read the PPM input from the Rx data line.
  if(ch1 > 1056){ /*Reject any PPM values that are outside the accepted range.
    I found the motor tends to jitter horribly if I don't have this line, as for some reason,
    0 is sent through every other time it reads.*/
  throttle = map(ch1, 1075, 1900, 0, 179); /*Map the values between 1069 and 1900
  (observed upper and lower limits of the throttle signal from the Rx) to the
  upper and lower limits of the ESC signal. These can be in the range 0-179, where
  90 is the middle position (0 movement if your ESC is reversible).
  Here it's only mapped from 80-100 in order to slow the motor down.
  You can adjust this to suit your motor or application.*/
  //NOTE: If your ESC only supports one-way movement, you'll need to
  //change any '90's in this program to '0' (for no movement) and adjust other values accordingly.
  }
  if(count < 500){
    esc.write(90);  //Essentially wait for 500 loops to ensure the ESC has bound.
    count ++;
  }else if(lowerSwitchState == LOW && throttle < 90){
    esc.write(90);  //If the signal is trying to push the actuator
    //past its lower limit, set the output to 90 for no movement.
  }else if(upperSwitchState == LOW && throttle > 90){
    esc.write(90);  //Same as above, but with upper limit.
  }else{
  esc.write(throttle);  //If all is well, relay the input Rx signal
  //to the ESC.
  }
  //NOTE: If your motor still tries to push past the limits, remember
  //to check that your motor (and actuator) direction is congruent with the
  //numbers here (0 for full reverse, 179 for full forwards).
  //If it isn't, you can switch two of your three motor leads around, or
  //swap the '<' and '>' in the above two 'if' statements.
}

slipstick

So does that mean it's working now? I'd just written a long reply but I guess it's not needed now.

Steve

danjr1

Not tested it yet so please do post it. I've ordered a new board just in case so will try again then. I'm a little more concerned about the wiring and whether thats right to be honest!

slipstick

O.k. first problem, the ESC has a 5V BEC. This should be connected to the Vcc pin on the Pro Mini NOT the Raw pin. That feeds an internal regulator and needs more than 5V for reliable use.

Get that going and see what happens - try the basic Blink sketch or add a few Serial prints to your code to check that it's actually doing something.

I'm still confused about your switches. That linked switch is NO (normally open) so that's not "push to break" it is "push to make". But mainly it only has 2 terminals and I don't see how you are connecting 3 wires (5V, ground and D2 or D3) to those two terminals. But we can sort that out when you have the power right and the Pro Mini actually running sketches.

Have you tested that arming code with your ESC? For some ESCs I've used that would stick the ESC into programming mode which is not very helpful.

Steve

danjr1

Will do on the power and I haven't tested anything yet. Spent all day yesterday sorting out the robot's drive so having a bit of a break today.

Apologies - the switches I use have continuity when the button isn't depressed so pressing the button breaks the circuit (tested with a multimeter). The wiring for both is + and - in parallel from the same pads as the arduino power in from the ESC then an extra wire is also soldered to each of the - tabs on the switches in addition to the - wires as the signal wires back to pins 2 and 3.

slipstick

So not the switches you linked to then? You're not exactly making it easy to help!

But unless there are some resistors in there that you haven't mentioned, what you have just said is that when your switches are not pressed they are shorting out the 5V from the BEC, i.e. the + and - from the BEC are directly connected together and you have D2 and D3 permanently connected to ground so they're never anything but LOW. So it's no wonder nothing works. You have very likely killed the BEC in the ESC.

The way those switches should be wired is one terminal to ground and the other to D2 or D3. Then you need pinMode for 2 and 3 set to INPUT_PULLUP and your checks should be for HIGH not LOW.

It sound like you have a multimeter, so check if you are still getting 5V from the ESC output.

Steve

danjr1

Sorry. The store i bought them from has closed down so I thought I'd found them.

ESC is still outputting 5v so its survived luckily enough. So I'm guessing the code will look something like this....


Code: [Select]

#include <Servo.h>  //Servo library to control your ESC.
Servo esc;  //Declare the servo name as 'esc'.
 
int ch1; //PPM input from the Rx (RC receiver) goes here.
int throttle = 0; //Serial input is mapped onto this value, should be within the range 0 to 179.
int count = 0;  //Variable to delay activation until ESC has bound.
int lowerSwitchState = 0; //Variable to track the status of the lower limit switch.
int upperSwitchState = 0; //Variable to track the status of the upper limit switch.
 
void setup() {
 
  pinMode(10, INPUT); //Input from Rx
  pinMode(2, INPUT_PULLUP);  //Lower switch (grey wire)
  pinMode(3, INPUT_PULLUP);  //Upper switch (green wire)
  Serial.begin(9600); //Start the serial output (for debugging purposes).
  esc.attach(11);  //Bind the ESC to the arduino, just as it would to a receiver.

}
 
void loop() {
  lowerSwitchState = digitalRead(2); //Read pin 2 (the lower limit switch).
  upperSwitchState = digitalRead(3); //Read pin 3 (the upper limit switch).
  ch1 = pulseIn(10, HIGH, 25000);  //Read the PPM input from the Rx data line.
  if(ch1 > 1056){ /*Reject any PPM values that are outside the accepted range.
    I found the motor tends to jitter horribly if I don't have this line, as for some reason,
    0 is sent through every other time it reads.*/
  throttle = map(ch1, 1075, 1900, 0, 179); /*Map the values between 1069 and 1900
  (observed upper and lower limits of the throttle signal from the Rx) to the
  upper and lower limits of the ESC signal. These can be in the range 0-179, where
  90 is the middle position (0 movement if your ESC is reversible).
  Here it's only mapped from 80-100 in order to slow the motor down.
  You can adjust this to suit your motor or application.*/
  //NOTE: If your ESC only supports one-way movement, you'll need to
  //change any '90's in this program to '0' (for no movement) and adjust other values accordingly.
  }
  if(count < 500){
    esc.write(90);  //Essentially wait for 500 loops to ensure the ESC has bound.
    count ++;
  }else if(lowerSwitchState == HIGH && throttle < 90){
    esc.write(90);  //If the signal is trying to push the actuator
    //past its lower limit, set the output to 90 for no movement.
  }else if(upperSwitchState == HIGH && throttle > 90){
    esc.write(90);  //Same as above, but with upper limit.
  }else{
  esc.write(throttle);  //If all is well, relay the input Rx signal
  //to the ESC.
  }
  //NOTE: If your motor still tries to push past the limits, remember
  //to check that your motor (and actuator) direction is congruent with the
  //numbers here (0 for full reverse, 179 for full forwards).
  //If it isn't, you can switch two of your three motor leads around, or
  //swap the '<' and '>' in the above two 'if' statements.
}

slipstick

You need to do some testing and then report what works and what doesn't. I see you've gone back to loop 500 times and you've not bothered with any Serial.prints. You make so many changes without testing anything that there's really no point looking at the code.

Fix the switches, test everything then report back if you're still having problems.

Steve

Go Up