Custom Pedal Assist Controller... Update from last year!

Joined
Apr 4, 2021
Messages
10
Hey Gang!

I created this post almost a year ago now, and I wanted to post an update! I am thankful to those who helped me in that thread, I was given some valuable insights that made this project possible.

Finished product first because I know you people want to look at stuff:
PXL_20220219_072411950.MP.jpg

To summarize, I set out to built a powerful, yet minimalist ebike, with as few components as possible, and as few wires as possible.

I landed on the following components:

The catch is that I didn't want to use a throttle, or any bulky items such as a screen or cycle analyst. And while the cycle analyst did perform the functions I needed, it was too large for my needs.

So I set out to design my own "MicroAnalyst" board that would input cadence and torque and output a throttle signal. And so begins my tale.

I started out with a simple design that was inspired by this post. Basically it is a circuit board that takes power directly from the 48V pack, steps it down to 10V which is suitable for the ERider Sensor, and then steps down again to 5V for the Arduino (an ATTiny85). (the schematic below shows version 2 that includes shunt resistor and amplifier, I will get to that point soon.)

E-BIke_V2_Schematic.PNG
View attachment E-BIke_V2_Schematic.pdf

First I had to install the motor and battery onto the bike. The Magic Pie 5 Golden Motor is surprisingly good quality. I was very happy with the fit and finish of the wheel I purchased. Only down side is I wanted the all black version but it was not in stock. Anyway, the Magic Pie has the monstrosity of a cable harness coming out of it. So the fist thing I did what hack it all down to the minimum number of wires required. There was a small circuit board embedded inside the harness, I just cut it out and used only the wires I needed.

PXL_20210523_064912469.MP.jpg

The only wires I really needed were Pwr, Gnd, and throttle. Though I did keep the original water proof connectors because they seemed nice, I still had to re-solder every wire in the bundle.

Here is what version 1 circuit board looked like (please excuse the terrible soldering job, these parts are tiny!!!):
PXL_20220219_072301598.MP.jpg

So long story short, version 1 did not work exactly as I had planned. Basically, this version only has two inputs: cadence and torque. The output, throttle signal, is sent back to the Magic Pie. The problem, is that the Magic Pie uses only "speed control" as opposed to "torque control". This means that the throttle signal, from 0-5V (actually 1-4V) represents the speeds that the Magic Pie will try to achieve, and it will use whatever torque is required to achieve that speed. I had very difficult time writing an algorithm that would translate only cadence and torque into a speed command when what I really wanted was to be able to control torque. Think about it, as a human, we output power in Newtons or lbs, and when we put that force onto a pedal, it turns into Newton-meters or foot-lbs. Want we want is to feel the bike respond according to our own torque input. Soooo, I was not able to write a stable algorithm that resulted in an "intuitive" feel. I got pretty close using a PID controller, but with lack of feedback from the motor, it was impossible to get a stable result. And with switching gears while riding, the feel would change a lot based on the current gear.

Now I knew exactly what piece of the puzzle I was missing: feedback from the motor. But how do you get the torque of a motor that does not provide you with such a signal? Use a DC current shunt resistor to measure the current going into the motor. Welp this meant that I have to make a revision of the board to include the positive battery lead that was fed to the motor. So if you look at U4 and R2 in the schematic above, there is a 5micro-ohm shunt resistor and an amplifier that now feeds into the micro controller. And because current into a motor is proportional to the torque of the motor, we now have the feedback we need to close the loop on this controller.

So 9 months pass, the project sits for a while until I finally got the itch to work on the bike again, I ordered the new circuit boards and this time I ordered a solder-mask stencil so that I would have a nicer finished product. Basically you spread solder paste onto the pcb like a silk-screen shirt machine and then place your components and stick the thing in a toaster oven.

Spreading the paste:
PXL_20220208_231930364.MP.jpg

Final product (yes the inductor is cracked, dropped this board on accident but I made three of them so this one I can fix at a later time):
PXL_20220219_235123217.MP.jpg

Given that this project was not really my highest priority in life (I just became a Dad!) I didn't really put much thought into the form factor or mounting, I just made the darn thing as small as possible. Here is what it looks like with wires soldered to it. I used 0.1inch pitch male headers to connect the wire so that it would be reasonably easy to un-solder them if needed (note the hot glue used to ensure to stray wires make contact with anything).

PXL_20220219_071429561.MP.jpg

Mounted to the bike:

PXL_20220219_071417114.MP.jpg

Cover installed:

PXL_20220219_071607717.MP.jpg

Connections made. I do want to clean this up in the future, but for now there are 3 connections, and easy to service. Maybe I'll just wrap them with cloth tape tight against the frame or something:

PXL_20220219_072508646.MP (1).jpg

Now for the fun part. With the current shunt resistor installed, I was able to write a very simple control algorithm using a PID control scheme. I wrote a few routines that sample the cadence, torque, and DC current in the background, and added a few digital low-pass filters to help smooth out some of the readings. And the main algorithm is shown here:

Code:
/**********************************Assist Algorithm************************************************************************/
static void PAS_algorithm(){
  // Convert the cadence assist Factor to a percentage (from 0-1).
  double cadence_factor = mapf(cadence_RPM, MINIMUM_PEDAL_RPMS, MAXIMUM_PEDAL_RPMS, 0.0, 1.0);

  // Convert the torque factor to a percentage (from 0-1).
  double torque_factor = mapf(torque_NM, 0, MAX_TORQUE, 0.0, 1.0);

  double assist_factor = cadence_factor*torque_factor;

  // Create the setpoint as a proportion of torque to current.
  Setpoint = CURRENT_SENSE_MAX_CURRENT*assist_factor;

  // Input is always the instantaneous current sense value.
  Input = current_sense_A;
  
  //Write the duty from the PI loop.
  analogWrite(throttle, Output);
}

The cadence is used to detect start-up conditions. Once the MINIMUM_PEDAL_RPMS are detected the cadence_factor becomes non-0 and if there is also torque detected, the torque_factor also becomes non-0. These two values are multiplied together to create the assist_factor, which grows from 0 up based on cadence and torque. The cadence caps out a MAXIMUM_PEDAL_RPMS so that once a certain cadence has been achieved, the algorithm relies 100% on torque alone. When the cadence dies back down, it brings the assist factor back down to 0. The PID controller will take the <Setpoint> and send modify the <Output> (throttle signal) until the <Input> matches the <Setpoint>. It took a little bit of "tuning" the PID parameters to get smooth response, but once I tried riding the bike for the first time, daaaaaaaaangggg, I was blown away. The start-up was super smooth and the bike felt like it was an extension of my body. The harder I pushed, the harder the bike pushed back. I felt like a superhero with super strength flying up the steepest hills in SF. No jerking, no stutters, just pure fun. There is still a little bit of tuning to do, but that is mostly playing with the low-pass filters of all the sensor inputs, and maybe a little more PID parameter tuning to really dial it in. The software and hardware designs can be found on my github page here.

Guys, I'm super stoked to ride this thing around. It blasts like I'm riding a motorcycle. I actually built this for my wife to ride so at some point I'll have to hand it over to her. Happy to answer questions, take criticism, or to see if there is any desire for others to use what I have built.

Thanks yall,

-Z
 
Sweet - Neat - Feat !!! .... surprised no posts to date being ES attracts dedicated.- inventive DIYers. Could it be that "cadence" DIY ebikers generally seem to prefer a Bafang mid-motor drive than a rear hub motor?

Agree that a 48V battery is more than enuf for say 90% of "cadence" powered ebikers. How goes it? Any changes you've made?
 
Apparently I missed this post since it wasn't part of the original thread, so none of those posting there or subscribed to that thread would have been notified of this one. :( I just now posted a link in that thread to this one so that anyone there will now be notified of this one.

But I'm glad you got a working solution, even though it's not much different in size vs the CA's actual control board (minus the case and display). ;)

I've saved this thread locally since the code/etc could help me out wiht my Nano Tidbits projects that include certain functions you probably had to code in to yours, or that will be close enough to give me a jumping off point, but apparently most of your code is not in this thread, making what is here not very useful for someone trying to replicate your success. :(

Is there another website that you hosted the rest of the code on?
 
:oops: Somehow I totally missed the link (because it's just part of the text instead of a separate link, and the blue the browser makes the link is so close to the black regular text that once my screen de-blues enough (f.lux) I can't see the difference. :oops:

Thanks for pointing it out to me. :)
 
Thanks! I think I need to re-commit the latest code. I also duplicated the project on another bike, I'll make another post!

Sneak preview:

1709589000984.png
 
Last edited:
Looks like you got the color you wanted.

Thanks for posting design and code.
I ended up just painting that motor black with plastidip spray-paint. Its holding up super well! I wasn't sure if the Magic Pie Black was painted black or had exterior covers on it, so i went with the standard version.
 
Back
Top