This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Any interest in STM32 Pypilot motor controller
Hi All,
I'm new to the forum for the most part but I thought I would start a thread on my build of a motor controller.

I am way more experienced with STM32 SoCs than with Arduino so I thought I would try porting Sean's motor.ino to STM32.

Here are the particulars:
RPi 4 running tinypilot.
Using the STM32CubeIDE which is very good and FREE.
Using the STM32F103 on the Blue Pill available for under $10 on ebay.
I have ported it to use the IBT-2 43 amp motor controller available from amazon for ~$5
Wiring it up is very simple. No need for PCB.

I have spent some time going through Sean's Motor.ino code and also McNugget's version to use IBT-2. A thank you for all their efforts. McNugget's exponential analog filters, YES, right out of the FFT book!
I have a working STM32 project code on a desk top simulator- 12V motor driving a rudder position pot.
Latest version includes a complete USB stack sending analog data for logging.
I am continuing to simplify the code removing timing related complications.
It currently communicates with Pypilot on server with no errors just fine. It just gets the motor controller timeout message since current isn't reported.

Runs at 72MHz, don't have to worry about timing much.
Currently run 5 channels of analog through DMA at a rate faster than needed.
12bit ADC.
Hardware PWM.
All setup code is handled by IDE at HAL level.
Everything runs on 3.3V. No 5V to 3V worries. (although opto-isollators aren't a bad idea)
IBT-2 has built in short circuit and under voltage protection.
Oh, did I mention, CHEAP.

What I have not completed:

Right now only Rudder analog is used, Other analogs- current, voltage, temp not implemented. I'm not sure I need them in my application.
I have not implemented rudder limit switches since I have analog rudder position.
I have not implemented a watch dog timer but this is trivial (on the list).
I am not implementing EEPROM  write and read as this is only needed for multi-controller environments.

If there is interest in this I will branch on github, if not it will be my solitary project. I would appreciate feedback on interest or those conversant with STM32 that would like to help.

You should post it on github if possible anyway because in the future inevitably people will look. I am curious as well.
OK, will do.

 I have also worked out the IS pins on the BTS7960 motor drivers to provide a single analog current input signal. After reading the datasheet for the driver I found the IS pin is a current source with ratio to load current, ratio is 8500:1. Connecting both IS pins together and adding the appropriate resistor to ground and then a RC filter to filter the extremely noisy current signal I get a nice, stable current signal just adding two resisters and a cap. I don't really need an extra circuit board for that!

So far, so good.

I am also thinking of porting it to one of the more powerful black pill boards. Not for the computing power but because many of the blue pills out there are Chinese knockoffs. I have also found a pill board using the STM32L412 which is the ultra-low power board which may be of interest for sail applications($11):

I have posted what I have so far to github:
Any feedback would be appreciated.

You might look at a more recent motor.ino if you want to implement the clutch and brake command at some point.. Otherwise I think it is good, and nice to avoid software pwm.
I reviewed the clutch and brake code and can easily add it but only if someone is interested. I seem to be time limited!
Retirement is a strenuous occupation. I have also run into an issue that STM32CubeIDE doesn't want to behave with git. When I make an STM workspace into a git repository the SWD debug doesn't work. Another pain in the ass. The reason I like the STM32 is the in circuit debug.

On another note: Can you point me to where your Pypilot control loop is defined? I would like to understand the PID implementation and the scale of P,I,D. It would be useful in analyzing response curves. I'm not sure how your PID ties into servo.speed and servo.period. I gather that servo period is a minimum command period but I don't know how this relates to PID. Do you have a road map to this command loop?

The scale for P I an D are in degrees relative to full speed movement. Essentially it gets filtered down so that the motor is not running all the time at very low speeds because this generally uses more power and is usually also inefficient. It is possible to implement a PID loop directly in the motor controller in the future if this is what you refer to, but pypilot generally outputs speed commands. If it is running with rudder feedback the PID loop is implemented in python, although this probably is a little sloppier.

Another option would be to write motor controller drivers for pypilot that support various open source motor controllers.
Thanks Sean. I wasn't really thinking of implementing the PID on STM32, I was just curious how it was working.

For a boat control, having the Rpi do it is plenty fast I would think and as you said it is filtered down to conserve motor running.

I haven't delved into the Pypilot code yet, only the motor driver. I'm pretty much a C guy and have done very little in python but isn't too late to learn.

I have added the watchdog timer to the STM32 project. There is a define in config.h to turn it off during debug. Any time you have a break point the timer runs out and it resets unless you define it out.
I meant the inner PID loop for position control could be on the motor controller. The basic pilot uses speed control so it wouldnt use it, but some other methods (and more in the future) will make use of position control which if that control loop were on the motor controller it could be slightly faster.
Oh, OK. I get it now.
Position control would be interesting to implement. In going through motor.ino I didn't see anywhere where command is interpreted as a position. Did I miss something?

Putting the PID loop on the controller, it would be interesting on STM32 to use RTOS and put the PID loop in a separate thread. I have used freeRTOS that comes with STM32 for a while and it is pretty good. I was actually thinking of developing my current version of motor on RTOS with separate threads for control and communication but that is another rabbit hole to run down. Thought I would keep it simple.

Forum Jump:

Users browsing this thread: 1 Guest(s)