Thruster control with Dual Shock PS Controller via pygame

We are using an Arduino Mega 2560 to control four T200 BlueRobotics thrusters with ESCs. We read input from a Playstation Dual Shock controller using a separate python script, and then communicate to the Arduino via the Serial port. Our python code is here and our Arduino code is here.

With the current code, we should be setting speeds between 1400 and 1600. However, we are getting some output that is setting it to speeds that are negative or very large and outside that range. The thrusters are connecting and disconnecting, and have erratic behavior.

Has anyone successfully controlled their thrusters this way? Would you be willing to take a look at our code and see if you see anything problematic? Thank you!

Hi @bifft, welcome to the forum! :slight_smile:

Sounds like a fun project!

Thanks for the context and the code links - they should be helpful with finding a useful solution :slight_smile:

Erratic thruster behaviour most likely means the ESCs are being sent unexpected control signals, which (without looking at your code) could either happen because

  1. Your Arduino code is ‘knowingly’ sending ‘invalid’ commands to the thrusters
    • possible if it’s passing on invalid values that it’s receiving from your Python code, or
    • it’s incorrectly converting requested values into invalid commands
  2. Your Arduino code is written using libraries or features that have conflicts you’re not aware of
    • this could be from library timer conflicts (like this one), or
    • there are interrupts that are interfering with the timing of the thruster signals

One of the most powerful debugging approaches is to break something into smaller components, test them individually, and then put them together, to see whether there’s a flaw in a particular component or if the issue is in the integration of a particular set of components.

From a quick look at your code I suspect it’s more likely to be case 2., but I haven’t tried reasoning through it carefully enough to completely rule out 1. either. I would recommend starting with some simpler Arduino programs that just prove each piece of functionality, e.g. write a program each to

  1. slowly turn a thruster (no input, just the Servo library and thruster setup code)
    • this should work fine - if it doesn’t then there’s likely a hardware issue with your Arduino or your ESC/thruster
  2. slowly turn a thruster while accepting Serial inputs (that just get printed back - no intentional effect on the motion)
    • this could fail with jitters when serial gets sent, in which case you have a potential culprit, and may indicate a timer conflict, or some significant interrupt times from the serial
  3. slowly turn all thrusters and servos (to make sure they’re all working, and have their pins correctly configured) while accepting serial inputs (and printing them back)
    • this is a more extreme version of the previous test
  4. use serial commands to set the positions of each servo, and the thruster speeds
    • this is like the previous test, but proving that direct control via serial is working as expected

If none of those fail, try writing a simple Python program that just stores and/or prints your controller outputs, so you can try to identify if those values are in a different range to what you expected.

If all of those work fine then there’s most likely a bug in your current pair of programs, but the good news is you’ve just built up several in-between steps that should form most of a new program that behaves equivalently. If there are failures during those tests though, I suggest you try to figure out ways around it, and then discuss how a given test failed, your thoughts and attempts to work around it, and any questions if you’d like further help (to resolve an issue you couldn’t work around, or to resolve new issues that came up afterwards) :slight_smile: