The fact that his has ended up being my first electronics project is a bit of a surprise. Besides LED lighting, my main focus has always been on audio electronics. More than anything though I'm probably just lazy in that I haven't got around to writing up any of my audio projects yet!
I suffer from Chronic Fatigue Syndrome as well as intense motion sickness. This makes public transport quite troublesome and with the CFS, walking long distances, running or cycling quite difficult. I have always enjoyed cycling, and having been a fan of Robert Llewellyn's Fully Charged Youtube channel, it occurred to me that you could probably buy conversion kits for ebikes and indeed, you can, so I purchased a kit from wooshbikes. This was the GSM 18A kit with a built in controller and is a mid-drive kit. This means that the motor turns the pedals and thus powers the bike via the standard gearing. Wheel hub motors drive the wheel directly and therefore work with a fixed gear ratio. This makes them less flexible in terms of going up hills and limits efficiency across a wide range of travelling speeds.
The GSM kit has been fantastic but over time I realised I want more, both in terms of motor control and how the pedal assistance works. The Bafang/8FUN conversion kits come with the ability to be reprogrammed and I originally thought of changing to one of those, but then I thought, why don't I go about building my own?
The GSM 18A kit also comes in two variaties. One with the controller and one without, the one without makes adding your own controller extremely easy and is also less expensive. The only issue with this would be, how exactly would I go about this?
Pretty obviously the heart of an electric vehicle is the motor controller and the motor itself. Brushless DC motors, such as is used here, do not naturally commutate and requiring pretty sophisitcated control electronics in order to spin. These can be fairly crude, such as is used in simple PC fans, or fairly complex, using powerful DSPs. DSP solutions have only really come on within the last few years though as processor efficiency and power have improved. Before then a real-time, DSP, solutions was impractical, but these days that is not the case, which is fantastic for the small EV sector.
Having researched some of the alternatives one of the best solutions turned out to be TIs Instaspin controllers, with their field orientation control (FOC) system for sine wave driven brushless motors.
So what is field orientated control?
Motors work due to the attractive and repulsive combinations of magnetic fields and as a motor spins these fields need to be constantly realigned in order to keep the motor spinning. If these fields aren't aligned properly then a motor will, at best, lose efficiency, or at its worst stop spinning altogether. Brushed DC motors control the alignment of these magnetic fields via a mechanical commutator. The position of the armature (the spinning part), relative to the stator (stationary part), is automatically handled by the brushes and commutator. As the motor turns the commutator controls which phases of the motor windings are energised and which are not. Brushless motors do not have this luxury and require different approaches to commutation.
The simplest of these is via hall effect sensors. These detect magntic fields and when imbedded within parts of the stator provide positional data for the armature. A controller can then use this positional data to time the energising of the motor phases and provide effective commutation. Given their simplicity there is actually nothing wrong with this method but it does have some limitations. The first is long term reliability. If any of the hall sensors fail, or if they should slip out of alignment, then the control of the motor becomes compromised. The second limitation is simply to do with the basic alignment of the hall sensors to begin with. Any misalignment will result in compromised commutation and thus affect the performance of the motor.
There's are very good reasons though why the small motors used for quad copters etc, do not use hall effect sensors. Quads are prone to crashing and any disloding of the hall sensors would render a motor, and the quad, useless. Then there's the matter of weight, complexity and the extremely high speeds that these motors are expected to work at. The larger, and usually slower, of the RC motors sometimes come with hall sensors, but almost all of them do not.
The way that these motors commutate is via a control method known as field orientated control. During normal operation two phases of a brushless DC motor are driven, whilst the third is not. During the off period of the disconnected phase the back EMF generated can be measured, and used, as a way of detecting the armature position, and thus, provide a way of commutating the motor.
Field oriented control systems are not all equal mind you. Some are quite complex and some relatively simple. To detect the back EMF current sense resistors are necessary, BLDC motors typically have three phases and you can use one, two or three current sense resistors in a FOC system. More is obviously better but the code processing the back EMF data can also vary considerably. Then there's the voltage measurements that you can include for the motor phases for additional control.
TIs Instaspin FOC system uses up to three current shunts whilst also monitoring the voltage of each motor phase. In additional to this TIs control system measures the back EMF continuously over the period of disconnection, then integrates it, providing extremely accurate positional data for commutation. Integrating the back EMF helps significantly with motor performance when the motor is operating at low speeds.This is one of the main drawbacks to FOC systems. When the motor isn't spinning zero back EMF is being generated, so they can have issues with starting up. This is especially true when needing to startup under load. Also, the slower a motor spins, the smaller the instantaneous back EMF is, making commutation harder. The total amount of energy however, per rotation of the disconnected phase, remains exaxctly the same, it's just that the amount of time per rotation goes up. This is where integration comes in. If you were to simply rely on the magnitude of the back EMF you'd have trouble at lower speeds, but if you integrate the EMF measurement you use the area under the curve for commutation, rather than the magnitude of the points drawing the curve. The area under the curve is exactly the same regardless of the speed that the motor is spinning so accurate commutation can be had both at low and high speeds.
The small motors for aviation do not suffer when using less sophisitcated control schemes because they are never required to deliver power at startup. Propellers typically provide extremely low resistance when spinning slowly, and during normal operation, they are rotating at very high speeds.
TIs Instapin FOC solution made a lot of sense in terms of my application so I decided it would be worth giving a go.
For an ebike though, the motor controller is only half of the story. While it is true you could add a simple throttle to the motor controller and be done with it, there's far more to a modern ebike than that. From a safety point of view an ebike generally requires switched brakes so that the motor unit is disengaged when you pull them. You'd figure that you'd release the throttle before applying the brakes, but in an emergency situation, with sudden breaking, you need the motor unit to shut off immediately, rather than when you choose to leg go of a throttle. Then there's the possibility of a throttle failure. The brake cut off being essential in regaining control of the vehicle. Safety asside, constantly having to operate a throttle is a bit tiresome and one of the reasons for having an ebike is so that you can at least get some exercise. Any decent ebike motor unit/conversion kit will include a set of sensors designed for detecting pedal motion, which can then be decoded and used to switch the motor unit on and off.
Then, as well as basic functionality, some kind of display is always nice. Everyone likes to know how fast they are going, or how far they've gone, but in the case of ebikes, there's a whole lot more you may wish to know. Firstly some sort of indicator for the charge level of the battery is a good idea. Just as you can drive a car to be more economic, the same is true for ebikes. If you're getting low on energy those are the times when you need to maximise efficiency. Another useful thing to know is how much power you are using. With a power readout you can take an active role in learning how to reduce the amount of power you are using, thus helping to maximise battery life in the event that you need to go further than usual, or you get into a situation where you cannot recharge your battery.
In the case of my ebike I use a continuously variable transmission from Nuvinci. These are fantastic for use with ebikes and they have a sweet spot for maximum efficiency in terns of energy transfer to the wheel. This happens at a ratio of 1 to 1. That is, one revolution on the input of the transmission, equals one revolution on the output of the transmission. Either side of parity the efficiency drops off, so knowing what ratio you're at is important.
The same is true for the motor unit itself, most electric motors operate most efficiently when running at high speed and in terms of mechanically commutated motors the speed of the motor (compared to its unloaded speed) is relative to the amount of power it is delivering. In the case of BLDC motors, where feedback is used, this isn't quite the case as the speed of the motor remains constant until a predetermined current limit is reached. Still, when you reach this limit the motor slows down and efficiency drops. Having a readout that lets you know when the motor slows down, relative to the desired rotational speed, is useful because it lets you know when you should shift down when climbing a hill.
The ability to drive a decent display, along with all the additional inputs, requires more than the Instaspin DSP can provide. As a result, an additional micro controller was chosen as a way of interfacing with the rest of the bikes inputs and peripherals. The motor controller works by performing, high speed, real time measurements, performing real time arithmetic and then sending real time control data to the MOSFET drivers. Commutation and timing is handled via precisely timed interrupts and if you add too much to the DSPs load it'll choke and fail to operate the motor. While adding to the DSPs main while loop could be done without too many consequences, adding to the interrupts would be a recipe for disaster. To function currectly all of the features, outside of the motor control, require interrupts to operate properly too, so a secondary processor makes a lot of sense. Given my familiarity with the PIC24 range of 16 bit processors from Microchip, I decided to go with the flagship, a PIC24E with 512k of program memory. Indeed it turns out that this was a good idea as a lot of its features and internal memory was utilised.
Going up to a 32 bit processor would be the next step and would certainly provide more scope for fancy graphics, but is otherwise unnecessary. Microchip have a new line of 32 bit processors coming out, the PIC32MK. These would be perfect as a replacement, giving more than is necessary for the project, but also making more sense too. The 32 bit architecture makes for far more efficient data storage when concerning the large data arrays for the display and would allow for more elabourate graphics. The 32 bit MK processors also include a dedicated floating point calculation unit which would be quite useful when concerning the calculations for speed, distance and time. To compensate for this numbers have to be scaled up for accurate computation such that numbers beyond the decimal point are moved ahead of it. The 32 bit MK processors include many more timers, which would be handy and finally they also come with a small amount of on-board EEPROM. Remembering certain parameters after switch off, is necessary for a project like this, such as to remember how far you have gone etc. This requires the use of a small amount of non volatile memory and while the standard flash memory, common to microcontrollers, can be used as such, it is very inefficient. Flash memory sectors can only be written a small number of times before expiring. To circumvent this EEPROM can be used, as it can be written many more times, but in the case of the PIC24E series, a separate memory chip is required. With the PIC32MK it is not, thus saving board space and potentially costing less, as the PIC32MK range is expected to be priced similarly.
So first up we have the display head unit.