Cruise control function with PIC16F886 for brushed dc motor

Jakhopp

1 µW
Joined
Apr 26, 2010
Messages
3
Disclaimer
Hey guys, this is my first post so please be nice! I have tried searching for a solution both elsewhere and on this forum but I have not found anything that will help me with my specific problem.

Introduction

I am currently working on my first E-bike! As you can see in this picture I have mounted the motor and battery on the bike.

The control system is entirely home made but the picture above is a bit old so it is not mounted yet. My finished circuit should look pretty much like this Don't mind the swedish gibberish at the right :) Anyway, the control system is PWM based which means I can adjust the speed with a throttle and it works pretty good i must say. The only problem i have experienced is when i start at a standstill in a slope with a large gradient and i try to get upwards. If i go full throttle then i will pretty much burn the MOSFET because of the large current surges. But i know about it now so it won't happen again :D

Now I am trying to implement a cruise control function that will pretty much consist of a SET and RESUME button. The cruise control and motor should also let go if I pull either of the brakes. I am currently using a bicycle computer to measure the speed but I replaced the magnet that came with it with this little fella. This means that it will respond 5 times more often so I had to “fool” the computer by setting the wheel diameter 5 times smaller. So why do I need 5 magnets instead of 1? Well the idea is that the sensor that supplies the bicycle computer with pulses is also going to be wired to a digital input on the PIC. This means that if I use 1 magnet then I have to wait for the wheel to rotate 360 degrees until it receives another pulse. I thought this would be a potential problem and data more often can't be bad right? But anyway on to the concept.

The concept
I will go up to a desired speed, for example 20km/h, by either using the throttle or pedalling. Meanwhile the cruise control has been in “standby mode” and the PIC has been counting pulses. By the help of timers I guess you can somehow calculate how many pulses per second the PIC receives. Anyway, when I see that the speed is pretty much 20 km/h on the bicycle computer I will push the SET button and the PIC should then save the pulses per second and use that as setpoint. The throttle should also relax or go into “sleep mode”. The PIC should then keep reading the pulses per second and compare the values to the setpoint. If the PIC reads like X pulses per second and the setpoint is Y pulses per second it obviously means that I am not travelling at my desired speed and the PWM needs to smoothly adjust until the reading is equal or very close to the setpoint. As you can see in this picture I have put these things inside the brakes so when I brake, the cruise control should go into standby and the motor will stop. When I press RESUME the cruise control will once again ramp up to the saved setpoint.

Some information about the components and applications I use
I am using a PIC16F886 and are programming in C. I am using the application mikroC pro for PIC when I program because I am not a very proficient programmer. In other words this suits me well because there are a lot of predefined functions. The motor I am using is a 36V BRUSHED dc motor and the battery is a LiFePO4 36V 10Ah.

Questions and conditions
Is there anything wrong with my concept, can I implement it?
If this is doable, is there anyone that has done a program like this in C?

If not, it would be very much appreciated if someone could help me a bit with the programming part. Especially the function that will make it possible to store the setpoint as pulses per second(or minute) or whatever works. If no one wants to help i guess you could always use this information to build your own E-bike without purchasing expensive controllers. Although buying a controller and a finished kit actually means that the bike will be up and running in 1 day instead of 100 :mrgreen:

Here is my current code that i use to control the speed of the bike with a throttle:
Code:
void pwm(void);                     //declare function
void main()
{
     OSCCON=0b01000111;      // Oscillator 1 MHz
     ADCON1 =0x80 ;          // Configure analog inputs and Vref
     TRISA =0xFF;            // PORT A to inputs
     TRISB =0;               // PORT B to outputs
     TRISC =0;            // PORT C to outputs
     Pwm1_Init(6000);       // Initiate PWM frequency to 6000Hz (the motor likes this freq)
     while(1)                     // loop the function!
     {
             pwm();
     }
}

 void pwm(void)
 {
    unsigned ad0;           // declare ad0 as unsigned type
    PORTC = 0x00;         // Set PORT C to 0, all bits 
    ad0=Adc_Read(0)>>2;   // Shift away 2 LSBs. ad0 now has 8 bit resolution (0 to 255).

    if(ad0<52)         //Vref is 5V. The Voltage over the throttle goes from 1.2-4.4V. 
       ad0=0;           //This makes it possible to have a duty from 0-100%!
    if(ad0>200)               
       ad0=255;      

    Pwm1_Set_Duty(ad0);   // Pwm duty cycle is now controlled via the ad0 input that the throttle is wired to
    Pwm1_Start();              // Start pwm!
    Delay_ms(5);                // short 5ms delay
}
 
It sounds workable, but I couldn't program to save my life so I can't help with that part. ;) (I actually have several ideas for which I need a programmer, because of that).

For the problem with blowing MOSFETs at high current, you could limit the current, since you have a sense resistor already in place. Your sample time just has to be fast enough to catch it and cut back the PWM duty cycle in time.

Alternately, as a hard current limit you could use an analog trip switch that cuts the gate signal to the MOSFET off, until the current drops. It could react *much* faster.

Or for an analog solution for not-quite-as-fast reaction that isn't just a cut-off, you could control the analog throttle input to the PIC with the current-sensing circuit, so that throttle input voltage is reduced as current begins to exceed a certain point. It could be done with op-amps easily, or transistors (but I'm not sure of the exact transistor circuit). The op-amp circuit would be more compact.

Paralleling a bunch of MOSFETs on a large-surface-area heatsink will also help with that, since no one FET has to take as much current.
 
I see what you are doing. Hmmm.... Cruise control for something like this will be interesting with non-freewheeling pedals - potentially harmful I would bet :)

The problem I can see is you have to be in the right gear, or your pedals will be spinning like mad, or slogging the motor. Perhaps a cadence + current based cruise control would be more appropriate? Using magnets on the chainwheel (or measuring back emf of the motor if you want to be clever).
 
For the problem with blowing MOSFETs at high current, you could limit the current, since you have a sense resistor already in place. Your sample time just has to be fast enough to catch it and cut back the PWM duty cycle in time.
This seems like a good idea!

Paralleling a bunch of MOSFETs on a large-surface-area heatsink will also help with that, since no one FET has to take as much current.
This too!

If i keep having problems with blowing the MOSFET i guess i will use these ideas since they seem to be the easiest to implement!Thanks for the help :D
 
I see what you are doing. Hmmm.... Cruise control for something like this will be interesting with non-freewheeling pedals - potentially harmful I would bet :)
I will actually have a freewheel so this should not be a problem :D

The problem I can see is you have to be in the right gear, or your pedals will be spinning like mad, or slogging the motor
Again the freewheel should solve this right?

Perhaps a cadence + current based cruise control would be more appropriate?
Exactly what do you mean with a current based cruise control? I thought of measuring the current the motor takes from the battery and try to use it in some way but i don't think it will turn out that well :(

Using magnets on the chainwheel (or measuring back emf of the motor if you want to be clever)
I have no idea how to do this :?
 
Back
Top