How to use servo and pwm motor libraries

I want to use servo motors and control the speed of DC motors on the Jetson Nano.

from adafruit_servokit import ServoKit # Servo library that works with Jetson
import Jetson.GPIO as GPIO			       # Part of PWM DC motor control 
GPIO.setmode(GPIO.BOARD)               # Error here

Error Description:

Traceback (most recent call last):
  File "brew.py", line 4, in <module>
    GPIO.setmode(GPIO.BOARD)
  File "/usr/lib/python3/dist-packages/Jetson/GPIO/gpio.py", line 317, in setmode
    raise ValueError("A different mode has already been set!")
ValueError: A different mode has already been set!

If there are any libraries that are simple to set up like ServoKit for the PCA9685 board, any easy fixes to have my current code run, or even a way to not use GPIO.setmode please let me know.

1 Like

hello carlos144,

could you please confirm the pin configuration, this error indicate that thereā€™s different mode has been set.

Iā€™m still learning all the lingo.
Is this what you are asking for?


I have my PWM output on pin33.

hello carlos144,

itā€™s GPIO13/GPIO3_PE.06 by default a GPIO pin, so, had you already configure this pin as PWM?
in addition, are you able to follow the sample in Topic 178959 for testing your pin?

I configured pin 33 as pwm and I havenā€™t touched any other pins as far as I remember.

As for the Topic you mentioned, I did indeed get it working just today.

When I attempted to run that code along with the adafruit library it would give me that error I mentioned above.

Itā€™s likely the AdaFruit library sets the GPIO mode already to whatever they want it to be.
Which means that you canā€™t use that library, and set the mode to something else.
Given that itā€™s open source, I imagine you can open up the code and read through it to figure out what theyā€™re doing, if you want to attempt to change it.

3 Likes

I ran down that rabbit hole. Afafruit sets it in tegra SOC mode in a file called pin.py.

Iā€™ve spent three days trying to make an l298n motor driver plus an Ada fruit 9826 16 channel pwm driver board work.

Pure. Hell.

I think it comes down to frequencies and once adafruit grabs the board youā€™re screwed because it will set the frequencies at about 50 and then the motor driver freaks out or doesnā€™t work.

Really really frustrated with this. All I want is a motor driver and some servos it doesnā€™t seem like it should be this hard.

2 Likes

Yea I totally agree, at least I wasnā€™t the only person with this problem. Did you manage to eventually make it work or no?

I was actually just about to sit down and give it about the second to last tryā€¦

So far and Iā€™m sure I will miss some things I have tried thisā€¦

I set up the 16-channel pwm card via i2c and I got all the servos to run I wanted.

I connected a pwm to the motor driver and freaked out everything. I got a soft reply on adafruit message board that you canā€™t run them off the same board because of frequencies. Servos like 50 or 60 and motors like a thousand or something like thatā€¦ so I fiddled around with the frequencies and found I could get intermittent working around 400 and pulled my hair out trying to make that work for a while unsuccessfully.

Next I decided to use the Jetson pwm pins and ran directly into the problem you found. If you really study the error you can track down the file and they set it In tegra SOC mode which is what the hell? Really?

Okā€¦So now you have to figure out the tegra names for those pins and itā€™s some obscure reference somewhere I managed to find great. So I called the pins and adfruit has grabbed not just the i2c but the whole gpio and now weā€™re back to frequencyā€¦

Currently I can make 16 servos tap dance. Iā€™ve actually connected a Bluetooth controller and I can spin my wheels forward or backward. But I canā€™t do both.

Afafruit suggested two pwm boards, you can chain them and address them but the servokit library is angles and the motor kit library doesnā€™t work on that board.

Seriously someone above my pay grade needs to look at this #@&$@#.

1 Like

Okay, so I was doing some searching and found this.
I havenā€™t tried it yet but it looks like to bypass the one frequency cap you could use two of these boardsā€¦
It will look a little funky but it should work out in the end Iā€™d hope so.

Biggest problem is the libraries wonā€™t do both. I decided to chunk the whole adafruit thing and just use a pie zero to control the motor with the l298n and the servos with inherent software pwm. Downside is you have to deal with jitters. Upside is it works.

So turns out I managed a very hacky way around this. I used two PCA 9685 boards to hit both the high and low frequencies at the same time since you can only use one per board.
Here is my set up:

This will likely be a temporary measure until I find a more elegant way around this library problem. Please let me know if yall have any questions or possible solutions.

1 Like

Hereā€™s my experience with Adafruit:

  • shipping and customer service is fine
  • hardware design is fine
  • documentation is fine
  • their libraries work well as documentation of how to bring-up the hardware
  • you will want to write your own drivers for their hardware, no matter what your host hardware is (e g, Arduino, Teensy, Jetson, PC) because their goals are not generally compatible with your own performance/integration goals

When it comes to motor drivers, the L298N is very low performance ā€“ high losses, runs warm, no built-in PWM, and so on. Itā€™s a 1970ā€™s level chip. There are many better options. Iā€™d suggest using a smarter controller (e g, a RoboClaw or Robotek driver) but I understand that itā€™s more expensive, and if dollars spent are your top priority, you would instead perhaps want something like a DRV88 based driver from either Adafruit or Pololu.

For servos, Iā€™ve given up on hobby servos using PWM. Anything that needs to respond in real time and have controllable torque, should use a better servo protocol. Either something like a Moteus controller or an ODrive, or an integrated servo motor + controller like the Robotis variants, is vastly preferable. Motion control is hard, and expensive, because it integrates with the real world, as opposed to ā€œpure softwareā€ which is much simpler and cheaper.

3 Likes

@snarky lots of good info there. I finally got my project working with two 9685 boards and a l298n. Wasnā€™t really looking for industrial grade just cheap and working and I got that but I appreciate the heavier duty/functionality info for later. šŸ˜šŸ‘

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.