Notifications
Clear all

Specifying the frequency of AnalogWrite() for motor control on RP Pico

21 Posts
4 Users
2 Likes
3,201 Views
(@redi45)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

Hi Everybody!

I'm building a small two-wheel driven autonomous car using Raspberry Pi's Pico. What a little jewel!

I'm using Arduino programming as I've got encoders on the wheel motors as Micropython/Circuitpython aren't great with interrups. Also, I'm planning to use PID to control the wheels. My motor driver is a Pololu's breakout of the TB6612FNG.

Unfortunately, I'm not as familiar with C++ as I am with python.

What I would like to do is set the frequency of analogWrite() that I send to my motor controller. My motors tend to growl if the frequency isn't 1.6K Hz.

I've searched around and cannot find the info. I think it has to do with scalers on a timer, but I fail to understand how to apply them to my issue.

Any help would be greatly appreciated!

Bo


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

Not my area of expertise, but I would first see if Bill has any YT videos about that, if he doesn't then a search in the library manager would be my next step followed by a simple google search. Good luck.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

Here is Bill's video https://dronebotworkshop.com/tb6612fng-h-bridge/

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 

@redi45

Have you considered using the pio of the pico for counting encoder pulses.  The assembler code can be run with mp.  Not that I've done this so I wont be much help in this, but it should be doable and I would love to see some good examples of using pio (and I confess this suggestion would be to use you as a guinea pig 😎).  I have also read about counting pulses using PMW, but again I've no idea, and I just made a mental note of this possibility.   

And another aside is the doc I link to below regarding reading encoders with micropython in case you've not perused it. 

https://github.com/peterhinch/micropython-samples/blob/master/encoders/ENCODERS.md

Of particular note in this doc maybe:

  1. encoder_rp2.py Version specific to Raspberry Pico RP2 chip. This uses the PIO and Viper code to achieve fast response - upto ~10K transitions/s.

I will have to leave it for others to comment on the setting of the frequency in C++

Have fun.

This post was modified 2 years ago by byron

   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@byron @redi45 I think using the Sparkfun library like Bill did eliminates the need for any of that. Check out the article at https://dronebotworkshop.com/tb6612fng-h-bridge/ Look at the 2nd sketch.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 
Posted by: @zander

I think using the Sparkfun library like Bill did eliminates the need for any of that.

It probably does, but it depends on whether @redi45 may prefer to use micropython as its more familiar  or if the wish is to delve more deeply in C++ .  It appears that the latency of micropython interrupts was a possible stumbling block, hence my suggestions for fast read of encoder pulses within micropython 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@byron Not knowing anything python I wanted to point out that even C++ knowledge isn't really needed. Hopefully he will look at Bill's example to determine if it does what he needs.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@redi45)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

@zander 

Great Video. However, it doesn't speak to the issue of frequency.

Thank you @zander!


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@redi45 Because it's built into the library I bet.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@redi45 Instead of speculating, run the sketch and see if it works for you.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@redi45)
Member
Joined: 5 years ago
Posts: 7
Topic starter  

@byron 

O!

I'll have ta check it out! squeek squeek(guinea pig sound)


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@redi45 @byron I just looked at the library code and do NOT see any manipulation of the PWM pin other than level. That doesn't sound right but as I said I am not an expert. Maybe it's time I bought that robot kit I have been looking at. Perhaps directing the OP question towards Bill will yield a gem.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
byron
(@byron)
No Title
Joined: 5 years ago
Posts: 1122
 
Posted by: @redi45

I'll have ta check it out! squeek squeek(guinea pig sound)

I had a look at the doc I linked to.  I had it bookmarked but now just had a good read of it.  A very impressive and knowledgeable write up of the subject matter I must say.   Plus I now see the example program for the rpi pico does all the pio stuff so you may not have to squeek too loud to get it all working. 😀 

Depending on the diameter of your wheels and the encoder pulse frequency my quick and dirty calc (and probably wrong) indicated your small bot would have to be travelling at twice the speed of sound before the counter maxed out.   Is it a bird, is it a plane, no its a redi45 superbot breaking the sound barrier (with a very loud squeek 😎)


   
Ron reacted
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6964
 

@redi45 Here is a link that covers it all. Unless you have a specific need to go deeper, just hooking up a pot to a PWM pin does all the work. The code involving PRE-SCALARS etc is all inside the arduino.

I suggest using Bill's sketch as a start and then maybe move on to learning the underlying hardware and assembler language (the hardest) to roll your own if you have spare time, meanwhile you have a working car while you either do or don't learn the hardware and low level programming techniques just for chuckles.

https://docs.arduino.cc/tutorials/generic/secrets-of-arduino-pwm

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
THRandell
(@thrandell)
Brain Donor
Joined: 3 years ago
Posts: 224
 

To err is human.
To really foul up, use a computer.


   
ReplyQuote
Page 1 / 2