Hi all,
I've been trying to build a system to control a pedestal using Arduino Mega 2560 with an Ethernet shield and USB host shield in order to use an Xbox controller or an analog joystick when Xbox controller is not plugged in. To move the pedestal with Xbox controller, L2 and R2 triggers must be squeezed before the left and right joysticks are used for safety reasons.
I also added 2 Digital-Analog converters for converting signals from Arduino to analog between 0 to 5V. After that a level shifter circuit will shift the voltages to -10V and +10V before for feeding the motor drivers.
There are 2 directions for the pedestal to move, Clockwise/counter clockwise (Azimuth) and Up/Down (Elevation).
There are 4 limit switches are utilized in order to disable the pedestal to move pass certain positions of Azimuth (Az) or Elevation (El). If one the limit switches is open, the pedestal cannot move pass that direction but still able to go back to the opposite direction; so the limit switch is shorted again and allows the pedestal to move back to and forth.
I also use a lookup table to make the Arduino operate faster than the equation (which is commented out in the sketch).
However, somehow the Arduino mega board gets frozen in Elevation direction frequently causing the pedestal to move in that direction even when the analog joystick or the Xbox controller joystick were released back to their default (middle) positions. On the other hand, the Az direction works fine without freezing.
I really don't understand why and need helps to figure out reasons why the Arduino keeps doing that.
I copied the code from Az section and used in the El section.
Thanks for any help!
IntegrationJoyUSB.ino (6.13 KB)
I don't understand why, on every pass through loop(), you send data to both DACs, even if it is the same value you send nanoseconds ago. Can you explain why that is necessary?
How ARE the limit switches wired? Using the internal pullup resistors would make wiring a lot simpler.
I thought it would be required to read all updated input signals from the joystick/xbox controller by placing sending the output data to the DACs. I need to constantly move pedestal for sweeping, not stationary most of the time.
The limit switches are hardwired to 4 pull-down 5k resistor individually to keep it at ground level if it from floating. The other terminals are connected to a common 5V rail. If the switches are open, the input signals to the Arduino are zero and if they are closed, then they are at 5V.
One thing that I don't understand is when pedestal is turned off (via safety switch), the limit switches are still closed and everything works fine, no freezing in either directions. But when it is ON, then only Elevation direction has this problem although the pedestal is only using the drive current from the separate power source.
sharpaxe:
The limit switches are hardwired to 4 pull-down 5k resistor individually to keep it at ground level if it from floating. The other terminals are connected to a common 5V rail. If the switches are open, the input signals to the Arduino are zero and if they are closed, then they are at 5V.
One thing that I don't understand is when pedestal is turned off (via safety switch), the limit switches are still closed and everything works fine, no freezing in either directions. But when it is ON, then only Elevation direction has this problem although the pedestal is only using the drive current from the separate power source.
I think it might be advantageous to draw out a schematic (if it's easier, using a pen and paper, then take a photo) and post it.
It is unclear to me what you mean when you say you have the pedestal turned off with a safety switch, and yet is is still able to move without freezing? How is it powered then? Again the schematic may help.
Are your switches configured in a normally closed mode?
When you say the pedestal freezes moving in one direction do you mean it:
- continues moving in the same direction as steered, or the reverse direction, or is it a bit random
- continues moving at the same speed, or slower or faster (which?)
- continues past a limit switch
Have you measured the voltage at the level converter output (with pedestal disconnected) to see if the 128 value really does result in a output voltage of almost 0? If it isn't quite 0, then maybe the momentum of the moving pedestal allows it to keep turning with a comparatively low voltage.
With regards to the code, you could try something like this untested code which might simplify your logic a little:
const int DeadZoneLowerLimit = 461;
const int DeadZoneUpperLimit = 560;
byte Ael = 0;
byte previousAel = Ael;
...
Ael = 128;
if ((YJoy < DeadZoneLowerLimit && bLM4 == HIGH) || (YJoy > DeadZoneUpperLimit && bLM3 == HIGH)) {
Ael = DACVal[YJoy >> 2];
}
if (Ael != previousAel) { // to keep PaulS sweet...
previousAel = Ael;
Wire.beginTransmission(PCF8591B); // wake up PCF8591
Wire.write(0x40); // control byte - turn on DAC (binary 1000000)
Wire.write(Ael); // value to send to DAC EL
Wire.endTransmission(); // end tranmission
}