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
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
Here is my current code that i use to control the speed of the bike with a throttle:
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
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
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
}