
PicOx - Open Source Motor Controller
For Light Electric PMDC Motors - Part 1 ...
Feb 2008
This page is the logical extension of the BFx
add-on brd built for the Dirt-E bike project
between Nov 2007 and Jan 2008.
Essentially all I want to do is clone the Yin Yu YK42-x
controller, but NOT as an analog duplicate. Rather as a Digital equivalent...
...if a simple micro controller (uController) like a PicAxe
08m were used in place of the LM324 quad comparator, it would open the door to
several possibilities for "Add-On's" that are impractical to develop
with an analog platform.
I'd like to be able say that I started off with the functional
block diagram below, but unfortunately I had to let myself get side-tracked with
I2C SD-Card readers and Wireless connectivity before I got back to basics.
As well I disclaim that the work below is through a
collaborative effort between myself and fellow builder located in California,
though I take responsibility for any errors or omissions of data, spelling
and/or grammar located on this site.

Doesn't look too bad...
I think that it can be done.

You don't need too much room, but it is one of those projects
that will be set-up for a while, at least in our case.
Builder "X" in Calif, is mirroring every move of
this site in near real-time to ensure that there are no glaring omissions and
that the results posted are easily replicated.
A PC, some bread brd for proto-typing the circuit, and some
parts...
There is a wealth of info on the PicAxe family of uControllers
at the link below...
I've spent so much time reading and reading the various posts,
I've found the answer to every question that has come-up in my mind without ever
having to post a thing.
The 08m is so entry level that there is only one other chip in
the family of PicAxe's that is of lesser capability and it should really be
called 'The Abacas"...
I like the PicAxe family because their cheap, easy to program
physically, and easy to program ie; writing the code.
The 08M may not be a heavy weight as a processor, but it
should be able to handle the objective of making a digital permanent magnet DC
motor controller. In fact I believe that by starting with such an entry level
processor, the core or kernel of the software will be have to be more thoroughly
thought out than if one was using a uController with more memory space as well
as more I/O lines.
I'd even go so far as to say that if I couldn't make the above
with an 08m it would be more of a reflection on me than a limitation of the 08m,
and moving to a larger processor would not improve matters much.
That being said, once a functional controller is built the
next version will be with the Far superior 18X class in the PicAxe family. Or
I'll have to hang my head in shame as the guy that couldn't even program an
08m...

Before going off on a tangent about the 18x, we have to figure
out how to get a program into the 08m...
The cable to the right is my solution, an old mouse cable, cut
with a FDD (floppy disk drive) power line added from a dead PC power supply...
You use what ever you've got handy, or order brand-new
pre-assembled cables, but some how the PC and the PicAxe have to talk.
Version 1.00.00

There is a schematic below that details the bread-brd set-up
to the left.
The seemingly haphazard selection of programming cable above
actually is a standard configuration that I use for all RS-232 based projects
that interface to a PC.

The code below is how I tested that the PC was actually
talking to the PicAxe, and that the PicAxe can talk back to the PC...
;Serial communications test Feb 8/08
symbol x=b1 ;initialize a counter variable
CountX: ;loop routine
sertxd ("PicOx Sez... Hello PC ") ;dump a text string to the PC via
rs-232
let x=x+1 ;increment counter
pause 1000 ;1 sec pause
if x<5 then CountX;compare x to 5 and loop if lower than
end ;end program

The "PBasic" interpreter that comes free with the
PicAxe, has a simple terminal window that I call-up to see what, if anything the
PicAxe has to say.
This webpage is not intended to act as a primer on getting
started on using the PicAxe 08m, as there are far better resources available, please
use them as I simply don't have the background to answer questions like "does
it matter how the power is hooked-up?"
Semi-Handy Tool
I don't think that it is essential that you have an
oscilloscope to build electronic projects, but it does open a window into your
circuits that otherwise makes diagnosing problems much harder.
I have been trying the PC Based program pictured below in the
hope that I could offer something other than just gloating that I have a scope
and recommending that everybody else have one because their so fantastic...
The image below is the actual serial data stream that was
being sent to the PC that is shown above in the Terminal screen... Pretty cool
huh???
The software does require a PC with at least a 1Ghz processor,
and 16bit sound card... The laptop I tested it on only had a monophonic "Mic"
jack so it can only display one waveform at a time.

The schematic for the simple 1 IC amplifier that I built is at
the link, below...
It is not my design, but worked well enough that I made a PCB
for it and have had good results.

The hardest part to find will be the BNC connector that the
standard scope lead plugs into.
If you would like the original Schematic and PCB layout in
ExpressPCB format email me...

Version 1.00.01
So at this point hopefully you have the most basic set-up at
least close to the above...
Per the functional block diagram at the very top of the page,
the logical 1st step would be to consider the numerous options available for
some sort of throttle control source.
Typically the Electric Bicycle and Scooter market are using
twist grip and thumb control units (as pictured below). These units can be
grouped into 2 classes, resistive or Hall Effect Sensor. Even as recently as 3
years ago there were more resistive throttles or "Pot-Boxes" (short
for potentiometer) than the now ubiquitous Hall Effect Sensor.



To determine how to wire in a Hall Effect Sensor throttle I
dis-assembled one to get the part number from the single electronic component.
It is an Allegro UGN-3503 sensor and any browser that can
display this page will find a data sheet by searching for it...
The sensor is biased or exercised by the 2 (two) magnets that
vary their positions relative to the sensor as either the throttle is rotated or
the thumb control is depressed.
The throttle assemblies pictured above typically can be
purchased for under $10 each from a variety of vendors... Even for an automotive
application I can see how it would be easy to adapt one these to a foot pedal
type throttle.
The schematic below shows both the interface for either a
resistive throttle or a Hall Effect Sensor.

Please note that the Hall sensor in the schematic is part #
UGN3505 NOT 5305 as noted...
Also the pin designations of the UGN3503 are per it's data
sheet NOT to the connector to the 08M.
Line them up by function Vcc to Vcc, Vout to ADC and Gnd to
Gnd.
The program listing below is how I tested that the 08m was
able to actually read the Hall effect throttle (if you decide to use a resistive
throttle you will have to make changes to the program, it would be safe to
assume that you're on your own...)
;UGN3503 Throttle test Feb 8/08
symbol fet=b1 ;initialize a Throttle output variable
symbol x=b2 ;initialize an x variable
main:
let x=x+1 ; Increment x by 1
readadc 1,b1 ;read fet voltage via ADC pin1
sertxd ("adc = ", #b1, " ") ;transmit value to PC via
terminal
pause 100 ;pause for 100ms
if x<100 then main ;compare x to 5 and loop if lower than
end ;end program

Again the terminal program was able to supply me with the
digitized throttle range...
After test 3 (three) different makes of Hall Effect throttles
I found that they all ranged between 45 to 48 on the bottom of their scale and
topped out at 217 to 220 at the top of their scale with a linear incremental
increase in values as the throttle is actuated.
If you decide to follow the next code example the throttle
that you decide to use should be determined to operate within that range of
values, otherwise the controller may respond erratically.
Ver 1.00.02
Since the picaxe is able to read the throttle, I decided to
add a small circuit that will make-up the "Gate Driver" block...
The ideal part to use in this application is a TC4427
non-inverting Gate Driver IC, but mine still haven't arrived yet so I opted to
use a quarter of a TC4468 quad driver. These parts are functionally equivalent
as configured on the schematic below. Though the TC4468 does take-up a bit more
room than the single driver.
The purpose of the Gate driver is to interface the 5V logic
level of the PicAxe 08M output to the gate level voltages required to drive the
gates of the MosFets that will form the output stage of the controller and
ultimately power a DC motor.
For very light loads of under 100watts, the interface could be
accomplished with a CMOS logic IC such as a 2 input AND gate configured as he
TC4468 for the non-inverting output.
As the voltages rise to 48V and up to 72VDC and the current
loads that the MosFets will have to deliver proportionally grow to 50Amps and
even up to 100Amps, the tiny picaxe 08m will be switching anywhere from
2.4Kwatts up to 7.2Kw of power. At these levels the addition of a dedicated gate
circuit start to come into play.
The link below is to one of the best references that I've
found on gate drive parameters...

Some points on the code below, please note that the actual ADC
(Analog to Digital Conversion) is being done at an 8-bit level that can offer up
to 256 unique values of the throttle position.
But the actual PWMout Command is being passed a parameter that
is derived from the throttle position that is 16-bits wide. This is related to
the maths that had to be done to accommodate the syntax of the PWMout command...
Unfortunately it's not quite as simple as saying PWMout @
15khz and vary the pulse width per the Hall Sensor... In actual fact I had to
resort to making a series of tables in Excel that would calculate the various
"Period" parameters and likewise the valid "Mark" parameters
that are dependant on the Period...
The real problem has to do with the fact that if you change
the freq (period) in the command, the range of pulse widths (Marks) are not necessarily
valid anymore, so I would advance the throttle and the output of the gate would
hit the top of it's range before the throttle position would, and then drop down
to a very low setting and advance for the remainder of it's movement.
I found part of the solution at the link below which is by far
one of the best explanations of how to manipulate the PWMout command...
http://www.fowkc.net/elec/pwmout.html
Mr. Fowkes was kind enough to include a javascript that
calculates the PWMout parameters more accurately than my spreadsheet.
;PWM test Feb 14/08
symbol hall=w0 ;initialize a 16bit Hall-effect Throttle
output variable
main:
gosub ThrottleRead;call Throttle Read subroutine
gosub PWMGate
gosub diagnostic
goto main ;return to top of main loop
diagnostic:
sertxd ("adc = ", #w1, " Hall
= ", #hall, cr, lf) ;transmit ADC & hall value
;to PC via terminal
return
PWMGate:
pwmout 2,66,hall ;output pwm on I/O #2 at
"period" 66 (15khz) with
;"mark" value of symbol
"Hall"
return
ThrottleRead:
readadc 1,w0 ;read 8bit value of Hall-Effect throttle
voltage via ADC pin1 and
;store in 16bit reg w0
let w1=w0 ;store 16bit value w0 in 16bit register w1
if hall<51 then gosub LowerScale;test throttle
position for zero/off ADC
;value under 50, on true call
LowerScale sub
if hall>215 then gosub HighScale;test throttle position for Full-On ADC value
;over 215, on true call HighScale sub
if hall>51 or hall=51 and hall<215 or hall=215 then Scaler;test throttle
position
;for operational
ADC
;value between 51 and
;214, on true call
;Scaler sub
return
LowerScale:
hall=0 ;set w0 value to zero for completely
"OFF"
return
HighScale:
hall=266 ;set hall value to 266 (Max Completely-On)
return
Scaler: ;routine to mathematically scale the ADC output range
51 to 214
;to the required PWM "Mark" parameter
scale of 1 to 266
;the math is limited to 16bit integer functions so variables are
;manipulated to accommodate this limitation
let hall=hall*10
let hall=hall-500
let hall=hall*16
let hall=hall/100
return
end
;end program
Obviously the spacing of the code is all screwed-up as it's
just been cut & pasted from the PBasic Editor, but I did want to include all
the comments that accompany the code to make it easier to decipher what is
happening.
One particular piece that I'm rather pleased with is the
"Scaler" routine that takes the range of 8-bit digital throttle
conversions and scales them to a range of 0 thru 266 requiring the 16-bit
"Mark" variable called "Hall"... Generally I don't go around
boasting about the fact that I can handle 4 function math, but the wrinkle was
that the math had to be done inside the PicAxe, and it can only handle integer
math (whole numbers, no fractions or decimal points), while the equation that
scales the Hall Sensor out put to the PWM Mark parameter is loosely Hall - 50
times 1.6 ...
A final note on the code above is that it is set to drive the
gate circuit at 15khz, any changes to that will require that there be
modifications to the Scaler routine as well.
The list below is saved from the terminal programs buffer,
this data was sent by the subroutine called Diagnostic, and it offered a great
way to see immediately if a change in the Scaler routine was working or not.
adc = 48 Hall = 0
adc = 47 Hall = 0
adc = 49 Hall = 0
adc = 49 Hall = 0
adc = 52 Hall = 3
adc = 54 Hall = 6
adc = 58 Hall = 12
adc = 62 Hall = 19
adc = 66 Hall = 25
adc = 69 Hall = 30
adc = 73 Hall = 36
adc = 75 Hall = 40
adc = 77 Hall = 43
adc = 79 Hall = 46
adc = 82 Hall = 51
adc = 85 Hall = 56
adc = 88 Hall = 60
adc = 92 Hall = 67
adc = 96 Hall = 73
adc = 100 Hall = 80
adc = 102 Hall = 83
adc = 102 Hall = 83
adc = 104 Hall = 86
adc = 107 Hall = 91
adc = 113 Hall = 100
adc = 116 Hall = 105
adc = 118 Hall = 108
adc = 121 Hall = 113
adc = 123 Hall = 116
adc = 127 Hall = 123
adc = 133 Hall = 132
adc = 136 Hall = 137
adc = 139 Hall = 142
adc = 142 Hall = 147
adc = 147 Hall = 155
adc = 151 Hall = 161
adc = 153 Hall = 164
adc = 154 Hall = 166
adc = 158 Hall = 172
adc = 162 Hall = 179
adc = 165 Hall = 184
adc = 170 Hall = 192
adc = 174 Hall = 198
adc = 177 Hall = 203
adc = 180 Hall = 208
adc = 183 Hall = 212
adc = 185 Hall = 216
adc = 187 Hall = 219
adc = 191 Hall = 225
adc = 193 Hall = 228
adc = 195 Hall = 232
adc = 198 Hall = 236
adc = 201 Hall = 241
adc = 203 Hall = 244
adc = 206 Hall = 249
adc = 210 Hall = 256
adc = 213 Hall = 260
adc = 215 Hall = 264
As an aside I found that the standard "CR, LF"
(Carriage Return, Linefeed) commands work fine with the serial transmit command
to the terminal...

This is yet another shot of the parts as their being added to
the proto-type circuit...
The larger TO-220 packaged parts located in the right center
of the bread brd are the MosFets, clamping diodes and additional voltage
regulation circuitry that will be added in the next installment.
The PC based Oscilloscope just couldn't display the waveforms
properly above 10Khz, so I dragged in my trusty TelequipmenT D61a... I think
that possibly if the laptop was faster and had a better quality soundcard it may
have been able to keep-up...


The images above illustrate the clean sharp snap of the gate
driver output at approx 20% and 80% throttle... the Scaler routine works so well
that the initial turn "ON" and full "ON" conditions are
accurately initiated at the very start of the throttles movement and at the very
end of it's movement.
So as time allows I'll replace the temporary TC4468 with the
TC4427 when it arrives, wire in the power electronics and add a current sensing
circuit that can over-ride the throttle position at a pre-determined point to
keep from destroying either the motor or controller if the motor is under too
much load.