Need help with controller for Moped electric cycle

TheMadMonk

New Member
I have an electric Moped style bicycle in which the main controller has failed. Since I am time rich and cash poor I decided to try to roll my own replacement.

I've figured out the electronics side of things and I think the code below will do what I want it to do, but i've never programed a micro-controller before and it has been a fair while since I've programmed anything else.

I would really appreciate it if someone could give my code a once over and make sure I haven't missed something obvious or have done something stupid.

Thanks in advance, BJ

Code:
'Motor controller for electric bicycle with active low inhibit and hall effect throttle
'sensor

    setint %00010000,%00000000    ‘ activate interrupt when pin4 only goes low

	low 2					‘ Motor Off
	let b1 = 128				' force acceleration from 0
main:
	readadc 1,b0				‘ read Throttle (values 128 - 169, 2.5 - 3.3V)
	if b0 > b1 then				' if higher than previous reading
		gosub accelerate		' accelerate smoothly
	else if b0 < b1 then			' if lower than previous reading
		let b3 = b0			' set motor speed directly
		gosub setspeed
	endif
	b1 = b0					' save throttle setting for comparision with next reading
	goto main				‘ loop back to start

accelerate:
	for b3 = b1 to b0
		readadc 1,b2			' test for throttle movement during acceleration
		let b2 = b2 + 2			' small bugger factor
		if b2 < b0 then			' if throttle backed off
			setint OFF		' bad stuff may happen if interrupt occurs in next 3 lines
			let b0 = b2 - 2		'
			let b1 = b3
			let b2 = 0
			setint %00010000,%00000000
 			exit			' exit if throttle has been backed off
		endif
		gosub setspeed
		pause 50			' 2 seconds to accelerate from 0 to max
		if b0 = b1 then exit		' trap in case braking interrupt occured during pause
	next
	if b2 = 0 and b0 > b1 then accelerate	'if throttle backed off accelerate to new setting.
	return

setspeed:
	let w2 = b3 - 128 MIN 0
	let w2 = w2 * 10 MAX 400
	if b0 = b1 then leave		 	' make sure brake hasn't been applied
	pwmout 2,99,w2
leave:
	return

interrupt:
	let b1 = 128				' force accelleration
	let b0 = 128				' from 0
	low 2 					‘ Motor Off
	Pause 10				' Debounce wait
	do loop until pin4 = 1			' loop until brake is released
	pause 100				‘ wait 1/10 second
	setint %00010000,%00000000		‘ re-activate interrupt
	return					‘ return from interrupt
 

jglenn

Senior Member
I am just getting started with the code so can't comment too much on that, looks involved. Consider a stripped down program for testing, just input the throttle and output the pwm fet drive signal, then loop. The motor should have a fast recovery diode across it. For testing just use a light bulb, or LED. That way you can try software changes without any chance of damage or stress to the motor. You need a bench test setup.

Accel controls on a moped, is it worth it? I'd just jam to full power and blast down the road. :D:D
 

lbenson

Senior Member
I can't comment on the program in general, but this line jumped out at me:

let w2 = b3 - 128 MIN 0

I don't think you will get what you intend here, since no picaxe number is ever less than zero (integer maths only). The maximum possible value of b3 is 255. Subtracting 128 from that leaves 127. If you have a value higher than 127, then you went negative. So I think you want

let w2 = b3 - 128
if w2 > 127 then: w2 = 0: endif

Play in the simulator to confirm.
 

TheMadMonk

New Member
I suppose I could use a simple loop and just not twist the throttle too fast, but I'm lazy, prefer to just hold it "wide open" and release the brake.

Need brake inhibit anyway to prevent overloading the motor or wrecking the brake pads.

And a soft start IS easier on the motor and batteries.

Convoluted/involved becasue I'm trying to cover all situations with the controls and their state changing at all points in the program execution.
 

Andrew Cowan

Senior Member
Looks OK. Any idea what you are going to do (in terms of eletronics) to cotrol the motor? Tip - do a search on this forum for FET driver.

Make sure you put a diode across the motor.

A
 

TheMadMonk

New Member
Opto - push-pull 639, 640 pair - 60V-15A MOSFET, and yes a whacking great diode across the motor.

And maybe one day another couple of MOSFETs and some whacking great electrolytic caps I picked up for a couple of bucks at a garrage sale to give me regenerative braking.

And Thankyou for taking the time to look at my code.
 
Last edited:

retepsnikrep

Senior Member
Most pwm motor controller have massive supply filter capcitors anyway. I suggest you add your capacitors before you blow your fets. Fit them as close as possible to the fets and use heavy gauge wiring for them. Even with a flyback diode the switching will still generate lots of noise and spikes. I also suggest you opto isolate the high current side from the logic control side.

I've been there and done all this in the past.

I don't know what frequency you will be switching the fets but they do require quite a significant current to turn on/off properly when supplying high currents, so the driver needs to be quite capable. I suggest about 20khz or slightly higher as that is beyond human hearing and motor won't buzz.

Monitor the switching on a scope. If they start getting hot and are well within there current rating then that indicates poor/slow switching. You need more drive current and sharper turn/on/off.
 

TheMadMonk

New Member
Definitely opto-isolation. And driving the FETs with a push-pull arangement lifted from a Silicon Chip motor controller project that used a TL494. That worked fine switching 8A @ 24V so shouldn't be a problem switching less than 6A @ 36V.

Would have used the same circuit again, but the only way I could get it to work with a Hall effect throttle last time was to kludge up a -ve supply rail with an AA cell. Fine for a kids electric go-cart, but not so good in a road bike that one day might decide to go wandering off on it's own because a silly little battery got a bit flat.
 

inglewoodpete

Senior Member
There was a short discussion on the MIN operator earlier in the thread:

let w2 = b3 - 128 MIN 0

Since the PICAXE only handles positive integers, you need to use the following to get the desired result:

let w2 = b3 MIN 128 - 128

Remember the PICAXE resolves an equation, like the above, from left to right: "b3 MIN 128" means "If b3 is less than 128, then make it = 128". After subtracting 128, w2 will never go 'below' zero.
 
Top