How to add a throttle to a Carrera Vengeance E Spec

Evian1040

Pedelecer
Jul 7, 2020
75
9
Getting "'pulseonlenght' was not declared in this scope" i know raj fixed it in his code with another one but what is the fix?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Got error 21 with code 9.1 on startup, even when programing with the arduino uno to the nano i saw once the compiled and uploaded the LED was flashing non stop.
 

Woosh

Trade Member
May 19, 2012
20,365
16,870
Southend on Sea
wooshbikes.co.uk
I think we'll have to stick to 8.1.

Getting "'pulseonlenght' was not declared in this scope" i know raj fixed it in his code with another one but what is the fix?
pulseonlenght - just a typo, pulseonlength is correct.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Ok so using 8.1, the pedals work exactly as the throttle but they are very sensitive (small touch and off they go, not like the original where the power goes with the user's strength on pedaling).
Now the current bugs would be:

- Sometimes the motor just freezes at full power and stays on for a long time or until you shutdown the bike.
- When the throttle is released the motor keeps going for about 2 full seconds.
 

rajeshtailor

Pedelecer
Jun 5, 2020
170
3
Ok so using 8.1, the pedals work exactly as the throttle but they are very sensitive (small touch and off they go, not like the original where the power goes with the user's strength on pedaling).
Now the current bugs would be:

- Sometimes the motor just freezes at full power and stays on for a long time or until you shutdown the bike.
- When the throttle is released the motor keeps going for about 2 full seconds.
Any error numbers? Or are we free of error numbers?
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
We are free raj, at least on the testing grounds.
We could still have an error while pedaling but this requires a proper test which i plan later at 6pm when i return.
 
  • Like
Reactions: Audio2

Woosh

Trade Member
May 19, 2012
20,365
16,870
Southend on Sea
wooshbikes.co.uk
So it’s really just down to fine tuning the code on the pedals?
Agreed. We should concentrate to get the pedalling code right.
What error did you still get on the pedals?

Woosh, do we require more testing with the picoscope?
the main problem is we haven't got a clean trigger on pin D5 so I have to guess at the waveform produced by the arduino DAC. That's where we can implement a better low pass filter as vfr suggested.
Try with various options on the trigger setting to see if you can get a solid line instead of a band.
It should look like this:
and we'll test the code to smooth out the ripples.

 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Try with various options on the trigger setting to see if you can get a solid line instead of a band.
It should look like this:
and we'll test the code to smooth out the ripples.

I'll see what i can do, i'm busy today but when ever i got spare time i will get to it.

On the bright side is we got a bike presenting no errors, if raj has the same output then this is very good news.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
I forgot to ask one thing, does the code have room for improvement for the throttle just like the original code? It's because the slightest signal sent from the throttle will make the arduino go full power.
 

Woosh

Trade Member
May 19, 2012
20,365
16,870
Southend on Sea
wooshbikes.co.uk
It's because the slightest signal sent from the throttle will make the arduino go full power.
part of the reason is the bike is not on the road, the motor needs little power to reach full speed.
I need the waveforms on pins D5 and A3 to compare the two.

Can you test this version 8.2? You can tweak ThrottleThreshold and ThrottlePerc later on real test.

Code:
// Version 8.2

const byte ThrottleThreshold = 100; // 1.95V
const byte ThrottlePerc = 20; // D5/A3 conversion ratio
const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = 5;  // PWM 980Hz
const byte CadenceOutputPin = 13;
const byte TorqueAtRest = 198;

int pulseoff = 25; // milliseconds when D13 is low
int pulseon = 25;   // milliseconds when D13 is high 4Hz
int cadencepulselength = 991; // in milliseconds
bool cadencestate = false;
bool pulse = false;
unsigned long startMillis;
unsigned long currentMillis;

void setup() {
  pinMode(CadenceOutputPin, OUTPUT);
  pinMode(TorqueInputPin, INPUT);
  pinMode(TorqueOutputPin, OUTPUT); //ControllerTorquePin
  pinMode(CadenceInputPin, INPUT); //PASCadencePin
  digitalWrite(TorqueOutputPin, LOW);
  startMillis = millis();
  // Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        // increment cadencepulselength
        if (cadencepulselength < 1000) cadencepulselength++;

        if (cadencestate)
        {
            // on cadencestate transtion from high to low, reset cadencepulselength
            if (analogRead(CadenceInputPin) < 512)
            {
               cadencepulselength = 0;
               cadencestate = false;
            }
        }
        if (analogRead(CadenceInputPin) > 512) cadencestate = true;

        // check throttle

        int torqueval = analogRead(ThrottlePin) * ThrottlePerc / 100; // Read the value from the throttle pin A0
        if (torqueval < ThrottleThreshold)  // disable throttle if voltage < ThrottleThreshold voltage
        {
        // I'm pedalling
        int pastorque = analogRead(TorqueInputPin) /4; // torque input A3
        if (pastorque < 80) pastorque = 80;

        // replicate torque and cadence

        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            // if I'm stopping for more than 1 second, set A5 to 3.8V to stop error 21
            if (cadencepulselength > 100) pastorque = TorqueAtRest;
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            pastorque = 0;
            // if I'm stopping for more than 1 second, set A5 to 3.5V to stop error 21
            if (cadencepulselength > 100) pastorque = TorqueAtRest;
            analogWrite(TorqueOutputPin, pastorque);  // sync pulse
        }
    }
    else
    {
        //I'm using the throttle
        cadencepulselength = 991; // fix throttle release
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, torqueval);  // testing
        }
    }
  }
}
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
part of the reason is the bike is not on the road, the motor needs little power to reach full speed.
I need the waveforms on pins D5 and A3 to compare the two.

Can you test this version 8.2? You can tweak ThrottleThreshold and ThrottlePerc later on real test.

Code:
// Version 8.2

const byte ThrottleThreshold = 100; // 1.95V
const byte ThrottlePerc = 20; // D5/A3 conversion ratio
const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = 5;  // PWM 980Hz
const byte CadenceOutputPin = 13;
const byte TorqueAtRest = 198;

int pulseoff = 25; // milliseconds when D13 is low
int pulseon = 25;   // milliseconds when D13 is high 4Hz
int cadencepulselength = 991; // in milliseconds
bool cadencestate = false;
bool pulse = false;
unsigned long startMillis;
unsigned long currentMillis;

void setup() {
  pinMode(CadenceOutputPin, OUTPUT);
  pinMode(TorqueInputPin, INPUT);
  pinMode(TorqueOutputPin, OUTPUT); //ControllerTorquePin
  pinMode(CadenceInputPin, INPUT); //PASCadencePin
  digitalWrite(TorqueOutputPin, LOW);
  startMillis = millis();
  // Serial.begin(9600);
}

void loop() {
    if (millis()>currentMillis) // run once every millisecond
    {
        currentMillis = millis();
        // increment cadencepulselength
        if (cadencepulselength < 1000) cadencepulselength++;

        if (cadencestate)
        {
            // on cadencestate transtion from high to low, reset cadencepulselength
            if (analogRead(CadenceInputPin) < 512)
            {
               cadencepulselength = 0;
               cadencestate = false;
            }
        }
        if (analogRead(CadenceInputPin) > 512) cadencestate = true;

        // check throttle

        int torqueval = analogRead(ThrottlePin) * ThrottlePerc / 100; // Read the value from the throttle pin A0
        if (torqueval < ThrottleThreshold)  // disable throttle if voltage < ThrottleThreshold voltage
        {
        // I'm pedalling
        int pastorque = analogRead(TorqueInputPin) /4; // torque input A3
        if (pastorque < 80) pastorque = 80;

        // replicate torque and cadence

        if (analogRead(CadenceInputPin) > 512)
        {
            digitalWrite(CadenceOutputPin, HIGH);
            // if I'm stopping for more than 1 second, set A5 to 3.8V to stop error 21
            if (cadencepulselength > 100) pastorque = TorqueAtRest;
            analogWrite(TorqueOutputPin, pastorque);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            pastorque = 0;
            // if I'm stopping for more than 1 second, set A5 to 3.5V to stop error 21
            if (cadencepulselength > 100) pastorque = TorqueAtRest;
            analogWrite(TorqueOutputPin, pastorque);  // sync pulse
        }
    }
    else
    {
        //I'm using the throttle
        cadencepulselength = 991; // fix throttle release
        pulse = true;
        if (currentMillis > (startMillis + pulseon)) pulse = false;
        if (currentMillis > (startMillis + pulseon + pulseoff)) startMillis = millis();     // reset timer clock
        if (pulse)  // SEND cadence and torque signal when D13 is high
        {
            digitalWrite(CadenceOutputPin, HIGH);
            analogWrite(TorqueOutputPin, torqueval);
        }
        else
        {
            digitalWrite(CadenceOutputPin, LOW);
            analogWrite(TorqueOutputPin, torqueval);  // testing
        }
    }
  }
}
I will give you the waveforms tonight even if I come home at midnight.
 

Woosh

Trade Member
May 19, 2012
20,365
16,870
Southend on Sea
wooshbikes.co.uk
I will give you the waveforms tonight even if I come home at midnight.
thank you but you don't have to rush.
Until someone takes the bike out for a road test, we don't know for sure which way to adjust the throttle.
In the new code 8.2, if the throttle starts too soon, we increase the value of ThrottleThreshold from 100 to 110, 120, 130, 140 etc until it feels right.
The other parameter ThrottlePerc adjusts the maximum power of the throttle, between 17-25.
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Sorry I'm late, only got home at 2am so this morning i worked on it right away.
Throttle: Channel A is D5, Channel B is A3
Pedals: Channel A is A2, Channel B is D13
I'm still using code 8.1

Readings 1.2
 

Evian1040

Pedelecer
Jul 7, 2020
75
9
Just took the bike for a spin using code 8.2
It works as expected! Pedaling works great and throttle as well, just needs fine tuning per the user's legs.
The main mission is complete.
Thank you very much woosh and raj for the help you provided. And thank you vrf400 for your help as well.
EDIT: And also thank you CdRsKuLL as you started this all.
 

Nealh

Esteemed Pedelecer
Aug 7, 2014
20,917
8,533
61
West Sx RH
Well done guys :D .
 

Advertisers