Code to control output On and Off pulse times with pots

hippy

Senior Member
If you intend to use the same Picaxe measure the flywheel velocity and to also control a Brushless DC motor with precision it may be
somewhat tricky due to the processor overhead.
It might be time to specify exactly what the project is and how it is intended to work, what sort of range it has to cover, and how accurately.

Generating fixed frequency pulse trains can be done by tweaking PAUSEUS values but the values are usually hand-tuned by trial and error while observing the outputs on a scope or logic analyser.

One would probably have to come up with some sort of look-up table and means of calculating the values if one wanted variable output frequencies. I am not sure how best that would be done or how good the results would be.

Adding something into the mix to allow that to be adjusted from something else would also be a challenge as that itself will affect the timing and values needed. As Goeytex says; could be tricky on a single PICAXE.

It could still be tricky with two PICAXE's, one indicating what is required, the other actually delivering that. One would need to find a way of getting data into the second and having it used without disrupting what it's doing, it's timing in any way. There are some possibilities but they would work best with byte values, not anything larger.
 

Goeytex

Senior Member
Yes, a complete and detailed description of the project is necessary in order for anyone to provide good advise.

Honestly, I see no way with a single Picaxe to to measure flywheel velocity and to use that data as feedback to precisely regulate the speed of a BLDC motor, assuming that is what the OP intends to do. But I don't like to assume.

The code I have simply uses pulsin to measure flywheel velocity and. If the velocity is too high it signals a fault and kills the ignition on a gasoline engine until the RPM falls back within range. (A rev limiter for racing karts). This would not be suitable for regulating the speed of an electric motor uner varying load conditions. But it could be adapted to turn a BLDC Motor off at a certain (programmable) RPM limit.
 

hippy

Senior Member
Honestly, I see no way with a single Picaxe to to measure flywheel velocity and to use that data as feedback to precisely regulate the speed of a BLDC motor
It might be possible to have the PICAXE bit-bang control the motor using PAUSEUS set from a variable, and have an input to the PICAXE which can adjust that variable from some input and do that with zero or minimal disruption to the timing loop. Could be hard to achieve though.

Then a second PICAXE could measure the speed of the motor and compare it to some required or reference value and determine if the motor speed needs increasing or decreasing, tell the motor controller to do that. This second PICAXE wouldn't necessarily need to have consistent timing.

In fact, that's probably how I would go about automatically determining the PAUSEUS values automatically for a desired frequency output without having to hand tune it. In that case though it wouldn't need to be zero impact as it's seeking correct values to use, not caring about disruption when changing timing.

Doing it well would be quite some challenge I would have thought. And it would depend on speed ranges, what responsiveness and accuracy were desired, and one would have to overcome or accept speed oscillations or hunting. It's effectively creating a kind of PID loop on a PICAXE.
 

Visser

Active member
Don't worry about that. The main aim is to solve any problems there may be, get things working. There always will be wrong alleys gone down.
I can't see any reason it wouldn't, other then the timing maybe not being entirely accurate.

It would help if you could explain in what way it doesn't work, whether it's just the timing which is out or something else.

If it's just a matter of timing then there will be two things at play here; that "PAUSE 10" may not give an entirely accurate 10ms period, and it and the other commands will also have some execution overhead. So your "High B.5 : Pause 10: Low B.5" time will be longer than 10ms, giving less than 50Hz. There's also a slight overhead in the DO ... LOOP which will make the two periods slightly unbalanced.

Some slight delay between turning off B.5/B.6 before turning the other on can be desirable as this avoids shoot-through if it's a H-Bridge circuit. Shoot-through is where both high and low side switches are active at the same time which shorts the +V to 0V which one doesn't want.

The key to getting the timing right and avoiding shoot-through is in the ordering of commands and tweaking the delay times, best done by PAUSEUS rather than PAUSE -
Code:
#Picaxe 18M2
#No_Data
Do
  Low B.5 : High B.6 : PauseUs 869
  Low B.6 : High B.5 : PauseUs 787
Loop
That seemed quite reasonable when I tried it but could do with more tweaking to get each high and low the same length and the frequency to be exactly 50Hz.

Uisng SETFREQ M32 gives much finer control over the timing as each PAUSEUS unit becomes 1.25us rather than 10us. Though one might then have to put a PAUSEUS between the LOW and HIGH to avoid shoot-through. More rough and ready than at 4MHz -
Code:
#Picaxe 18M2
#No_Data
SetFreq M32
Do
  Low B.5 : High B.6 : PauseUs 8000
  Low B.6 : High B.5 : PauseUs 7800
Loop
[/QUOTE



Good day. I tried the code above. The last one gives problems on the pic when driving the H bridge inverter. The 2 nd last one works good
Will it be possible to do code to introduce dead times like in my picture?
I need that dead times to allow the reverse inductive pulse from the transformer to discharge through the mosfet build in diode before the other 2 mosfets of the H bridge switch. I tried but just dont succeed
picaxe timing.jpg
 
Last edited:

Visser

Active member
I seem to get it right using the pwm wizard but would like to use B5 and B6 outputs And this is 61Hz It cannot do PWM at 50Hz
It seems like nobody has an answer for me
I was also wondering if Picaxe can do SPWM signals for a modified sine wave inverter or should I rather try Arduino?
 

AllyCat

Senior Member
Hi,

Probably no answers because you're largely asking for the "impossible". The PWM outputs are generated by the base PIC hardware with the well-known limitations that the pin functions are fixed and the minimum frequency is just above 60 Hz (at SETFREQ M4). But there are a few "work-arounds", depending on your overall requirements. Firstly, the 18M2 is not a good choice with only two PWM named output pins, the 14M2 and 20M2 have 4 PWM outputs and possibly also some HPWMOUTs. There is also the DSM module which can "copy" some internal/input signals to its output, but that won't help if you've already "fixed" which pins you want to use for the PWM outputs. Finally, you could generate a 50 Hz output by using SETFREQ M2 (use the Wizard for 100 Hz, M4), but that will compromise the general processing speed.

As for SPWM, it very much depends what switching frequency you want to use and the duty cycle update rate (which is one of the factors that will determine the "purity" of the sine-wave). There are certainly "challenges" in using a PICaxe (or probably almost any microcontroller): You might be able to update the "sine" value by reading a lookup table about every millisecond, (but not using PWMDUTY, which is too slow) and you may need some clever coding to keep the frequency close to 50 Hz. Of course, most PICaxe chips have at least one Comparator, so in principle you could generate SPWM from the comparator by using a PWM (HF switching) output to generate a triangle wave and the "program" to synthesise a sine wave from a combination of lookup table, analogue filtering and timer/polling/interrupt, etc.. Maybe easier on an Arduino, maybe not......

Cheers, Alan.
 

Visser

Active member
Hi,

Probably no answers because you're largely asking for the "impossible". The PWM outputs are generated by the base PIC hardware with the well-known limitations that the pin functions are fixed and the minimum frequency is just above 60 Hz (at SETFREQ M4). But there are a few "work-arounds", depending on your overall requirements. Firstly, the 18M2 is not a good choice with only two PWM named output pins, the 14M2 and 20M2 have 4 PWM outputs and possibly also some HPWMOUTs. There is also the DSM module which can "copy" some internal/input signals to its output, but that won't help if you've already "fixed" which pins you want to use for the PWM outputs. Finally, you could generate a 50 Hz output by using SETFREQ M2 (use the Wizard for 100 Hz, M4), but that will compromise the general processing speed.

As for SPWM, it very much depends what switching frequency you want to use and the duty cycle update rate (which is one of the factors that will determine the "purity" of the sine-wave). There are certainly "challenges" in using a PICaxe (or probably almost any microcontroller): You might be able to update the "sine" value by reading a lookup table about every millisecond, (but not using PWMDUTY, which is too slow) and you may need some clever coding to keep the frequency close to 50 Hz. Of course, most PICaxe chips have at least one Comparator, so in principle you could generate SPWM from the comparator by using a PWM (HF switching) output to generate a triangle wave and the "program" to synthesise a sine wave from a combination of lookup table, analogue filtering and timer/polling/interrupt, etc.. Maybe easier on an Arduino, maybe not......

Cheers, Alan.
Thank you Alan
I will try more and post here
 

Visser

Active member
I don't know just what you are trying to do [switch high power MOSFETs?] but I recalled Don Lancaster's work started many years ago https://www.tinaja.com/mssamp1.shtml which might interest you.
Thank you I will check it out. Yes H bridge igbt switching through isolated power supply and mosfet drivers. Square wave inverter. Im investigating to see what extra battery life I can get with ac load recovery back to the batteries
 

Visser

Active member
Thank you I will check it out. Yes H bridge igbt switching through isolated power supply and mosfet drivers. Square wave inverter. Im investigating to see what extra battery life I can get with ac load recovery back to the batteries
Oh and Im also studying how to build a pure sine wave inverter. Your link will help a lot
 
Top