Skip to main content
Topic: Servo Testing using PWM Generator (Read 6268 times) previous topic - next topic

Servo Testing using PWM Generator

Hey All,

I love using my BP to debug random circuit projects, and I've only just run into a shortcoming I (think I) can solve:

The PWM generation works great for most applications, but when I try to use it for servos, I find it can't go that low.

The PWM Generator prompt asks for a signal frequency in KHz, but I Need 50Hz (with 5%-10% duty cycle for fullLeft-fullRight).

I tried to write a macro that would just do the delays (go high, wait 1ms, go low, wait 19ms) but this turned out to be >1000 characters (since the delay is in us instead of ms).

So I'm proposing two changes:
* Be able to enter PWM frequency in Hz
* Be able to enter delay in ms

Either as new commands or new syntax for the existing commands (any suggestions?)

As I'm new to this, I'm guessing the protocol is: Checkout source, build/modify/test, and submit a patch when I'm done?

Thanks in advance,
~Alex

Re: Servo Testing using PWM Generator

Reply #1
At the very least you can grab the source and see what you can come up with. If you do get something working then I'm sure submitting a patch or checking in your changes can be worked out with the moderator.

Re: Servo Testing using PWM Generator

Reply #2
There will be a command that will wait 1ms. I haven't got the time to implement it yet, but it is on its way.

I don't think the PWM will go as low as 50Hz, I beleive the minimum is around 1Khz.

Re: Servo Testing using PWM Generator

Reply #3
If you're a PIC guru, you could rewrite the firmware to lower the clock rate enough that 50 Hz might be possible. Most PIC variations have a really slow 31 kHz oscillator on board, but you'd only want to use that briefly - long enough to do your measurements and then bump back up to full speed. But this would not be a trivial feature to add to the Bus Pirate. The cool thing about open projects is that you can build many things on an open hardware platform that may have never been considered, although it means you have to write your own firmware which might break from the standard feature set.

Re: Servo Testing using PWM Generator

Reply #4
I believe the newterm branch includes a MS delay.  I'm not 100% sure, but I think the PWM library has all the settings possible with the timers used in the PWM hardware and it can't go slower (but it's been a long time since I wrote it, it could be something else, I know SPI module is like that). Maybe you can get better control of the PWM through the binary mode (maybe, I forget), but that's not really helpful.

Patches are no problem, microcontroller developers are so rare (or the Bus Pirate relatively unpopular) that we have the luxury of a very relaxed patching practice (maybe some would just call me lazy). Post it or email it, pm me if I don't notice. I'll apply the patch and maybe make some style changes. If it doesn't run against existing features or take a huge chunk of flash, I usually never reject a patch.
Got a question? Please ask in the forum for the fastest answers.

Re: Servo Testing using PWM Generator

Reply #5
You all beat me to the reply :) Yes, the clock switching is a possibility. I think it would be a pain to implement because it would kill the serial coms settings (and corrupt/no I/O would be possible). It could be possible if the UART buffer is polled until it's empty, then reduce the clock, do the operation, then increase the clock again and resume serial coms. However, this is going to be crazy erratic in terms of proper timing (and the goal is precise timing, so...). The best would be if the peripherals could run from selectable clocks, then we could have hardware with a 31.768khz secondary osc and run timers from it when slow + accurate is needed.
Got a question? Please ask in the forum for the fastest answers.

Re: Servo Testing using PWM Generator

Reply #6
[quote author="rsdio"]
If you're a PIC guru, you could rewrite the firmware to lower the clock rate enough that 50 Hz might be possible. Most PIC variations have a really slow 31 kHz oscillator on board, but you'd only want to use that briefly - long enough to do your measurements and then bump back up to full speed. But this would not be a trivial feature to add to the Bus Pirate. The cool thing about open projects is that you can build many things on an open hardware platform that may have never been considered, although it means you have to write your own firmware which might break from the standard feature set.
[/quote]

I am by no means a PIC guru, this is the first PIC platform I've tried to dive into the code in.

Really, I was just gonna bit-bang it: write high, spin-wait, write low, spin-wait.

I'm working on becoming an AVR guru thought :-)

Re: Servo Testing using PWM Generator

Reply #7
I think this might warrant it's own topic but (sorry for the n00b question!):

How do you build the source?

I checked out the svn trunk (from googlecode) and I've made the modifications I want (new command for millisecond waits and servo-specific stuff), and now I want to test it... and I can't find buildscripts or antfiles or buildfiles.

Or some kinda tutorial?

Thanks in advance
~Alex

Re: Servo Testing using PWM Generator

Reply #8
[quote author="ajray"]Really, I was just gonna bit-bang it: write high, spin-wait, write low, spin-wait.[/quote]Not a bad idea. But I would recommend setting up a timer with a precise period, write high, wait for a certain number of timer cycles to fire, write low, wait for a certain number of timer cycles. I haven't looked into the Bus Pirate firmware too deeply, but I think there might already be a timer set up.

Re: Servo Testing using PWM Generator

Reply #9
ajray: the bus pirate manual is here http://dangerousprototypes.com/bus-pirate-manual/ and the specific about compiling the sourcecode: http://dangerousprototypes.com/2009/09/ ... -firmware/ . If you are using windows and ds30loader (the GUI client) you don't need to do the export step and you can use the generate hex file. otherwise export 0x0000-0xA7FF and _no_ config bits.

[quote author="ajray"]
I think this might warrant it's own topic but (sorry for the n00b question!):

How do you build the source?

I checked out the svn trunk (from googlecode) and I've made the modifications I want (new command for millisecond waits and servo-specific stuff), and now I want to test it... and I can't find buildscripts or antfiles or buildfiles.

Or some kinda tutorial?

Thanks in advance
~Alex
[/quote]

Re: Servo Testing using PWM Generator

Reply #10
[quote author="Sjaak"]
ajray: the bus pirate manual is here http://dangerousprototypes.com/bus-pirate-manual/ and the specific about compiling the sourcecode: http://dangerousprototypes.com/2009/09/ ... -firmware/ . If you are using windows and ds30loader (the GUI client) you don't need to do the export step and you can use the generate hex file. otherwise export 0x0000-0xA7FF and _no_ config bits.
[/quote]

I just got MPLAB working under Linux, and I'm able to export.

However, I'm confused as to the configure bits. Excerpt from that page:
Quote
The compiled firmware must be exported in order to work with the bootloader. In MPLAB, go to File->Export… Make sure that the Program Memory and Configuration Bits boxes are checked. Make sure INHX32 (Intel 32bit HEX) is selected on the File Format tab. Click OK to export the firmware.

Do I not need them because I'm exporting? Or should I use them?

~Alex

Re: Servo Testing using PWM Generator

Reply #11
Config bytes are already programed. If you try to upload new ones, well that might work, but most probably you are going to break something :)
So you dont want to program them

Re: Servo Testing using PWM Generator

Reply #12
the bootloader (on the pic) prohibits to write to the last page (the configbits reside there). But it gives rather cryptic errors/strange behauviour if you leave them in. Also it depends on the client you are using (pirateloader or ds30loader) what happens.

Like I said before don't export the config bits, just the program memory from 0x0000 -m 0xa7ff.

Re: Servo Testing using PWM Generator

Reply #13
The guide was written for the v2/3 branch I believe, I should do an update for the 4.0.

Sjaak is a master compiler wizard and has it setup so the generated .HEX is made without the last page (and config words).

I compile and then export->set range to 0xa7ff, uncheck export config bits. Then I use the exported .HEX.

The v4 bootloader lives in the last page with the config bits (0xa800-0xabfe (something?)), and it shouldn't allow itself to be overwritten. It will thrown a bootloader protection error, but as long as it programs in sequence (0 to the end) all the previous pages get written and it should work OK anyways (you just have to contend with the error message).
Got a question? Please ask in the forum for the fastest answers.