How to add a throttle to a Carrera Vengeance E Spec

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
Just executed the test and received error 21 on start up.

However it looks like the pastorque value isn't being set to the TorqueAtRest so that condition must not be met.

This line looks like its not being executed.

if (cadencepulselength > 990) pastorque = TorqueAtRest;
Because if I replace this line

analogWrite(TorqueOutputPin, pastorque); // sync pulse
with

analogWrite(TorqueOutputPin, 198); // sync pulse
I don't receive any errors on start up.

I'll try and debug this later with the serial debugger.
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
However it looks like the pastorque value isn't being set to the TorqueAtRest so that condition must not be met.
I agreed. Can you change this line: int cadencepulselength = 0;
to:
int cadencepulselength = 991;

so to preset the condition to at rest.
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
I agreed. Can you change this line: int cadencepulselength = 0;
to:
int cadencepulselength = 991;

so to preset the condition to at rest.
So a new test and results are:

1. No error on start up.
2. Start throttle, no errors.
3. Stop throttle, error 22 received. (last time it was error 21).
4. Recycle power (turn off and on)
5. No error on start up.
6. Start pedalling, no errors.
7. Stop pedalling, receive error 21.
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
we get closer.
does the throttle and pedaling behave like you expect?

Could you also change this line to shorten the leadtime before we assert TorqueAtRest:

if (cadencepulselength > 990) pastorque = TorqueAtRest;

to

if (cadencepulselength > 100) pastorque = TorqueAtRest;

There are two places where they are found.
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
we get closer.
does the throttle and pedaling behave like you expect?

Could you also change this line to shorten the leadtime before we assert TorqueAtRest:

if (cadencepulselength > 990) pastorque = TorqueAtRest;

to

if (cadencepulselength > 100) pastorque = TorqueAtRest;

There are two places where they are found.
So yeah the throttle and the pedaling seem to behave as expected, I'll know better when i actually sit on the bike and go out for a ride, but it seems normal.

And I've updated the code. We are definitely not far from the finish line now!

However getting error 22 when releasing the throttle instead of 21 makes me think there is a ratio between the torque and cadence that need to match throughout.
 

Evian1040

Pedelecer
Jul 7, 2020
73
9
So yeah the throttle and the pedaling seem to behave as expected, I'll know better when i actually sit on the bike and go out for a ride, but it seems normal.

And I've updated the code. We are definitely not far from the finish line now!

However getting error 22 when releasing the throttle instead of 21 makes me think there is a ratio between the torque and cadence that need to match throughout.
Spot on raj! If we're getting error 22 then there's an unbalance somewhere (in this case it would be HZ does not match the voltage)
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
revised code:

Code:
// Version 8

const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = A5;
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) / 4; // Read the value from the throttle pin A0
        if (torqueval < 75)  // disable throttle if voltage < 1.5V
        {
        // 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, 0);  // sync pulse
        }
    }
  }
}
To fix error 22 when releasing the throttle, I reset the cadencepulselength = 991.
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
revised code:

Code:
// Version 8

const byte ThrottlePin = A0;
const byte CadenceInputPin = A2;
const byte TorqueInputPin = A3;
const byte TorqueOutputPin = A5;
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) / 4; // Read the value from the throttle pin A0
        if (torqueval < 75)  // disable throttle if voltage < 1.5V
        {
        // 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, 0);  // sync pulse
        }
    }
  }
}
To fix error 22 when releasing the throttle, I reset the cadencepulselength = 991.
I have tested this just against the throttle.

1. On Power up no errors.
2. Run the throttle, no errors.
3. Release throttle returns error 22 after approx 3secs.
 

Evian1040

Pedelecer
Jul 7, 2020
73
9
I have tested this just against the throttle.

1. On Power up no errors.
2. Run the throttle, no errors.
3. Release throttle returns error 22 after approx 3secs.
I know you tested this just for throttle but any chance on the pedals to see how it reacts?
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
I know you tested this just for throttle but any chance on the pedals to see how it reacts?
Just done the test with the pedals, exactly the same behaviour. So error 22 after you stop pedalling and as the speed drops close to 10mph you then get error 22.
 

Evian1040

Pedelecer
Jul 7, 2020
73
9
Now since i haven't tried the new code yet (use the bike to go to work), when you're pedalling does it act just as your wired straight to the controller? As in if you put more work, the motor puts more work? (sync to the user peddling)
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
Now since i haven't tried the new code yet (use the bike to go to work), when you're pedalling does it act just as your wired straight to the controller? As in if you put more work, the motor puts more work? (sync to the user peddling)
I'm not riding the bike when testing, but it feels normal like its wired straight to the controller, I'll get a better sense of that when i ride the bike. But it seems like its functioning as normal so the more work you put in the more the motor puts in.
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
can you check the voltage on pins:

A0
A2
A3
A5

after error 22 shows up?
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
Thank you for doing that.
It's a bit strange though.
Both you and Evian previously had A3 on 3.86V.
now 1V.
Can you think of any reason?
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
Thank you for doing that.
It's a bit strange though.
Both you and Evian previously had A3 on 3.86V.
now 1V.
Can you think of any reason?
ok so I have redone this. Checked the values after error 22.

And the readings show 3.85 for both A3 and A5.
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
we need to figure out what condition produces error 22.
 

rajeshtailor

Pedelecer
Jun 5, 2020
112
3
we need to figure out what condition produces error 22.
Let me have some time to debug the code see if I can work out what happens at a code level prior to the error 22. I did notice it appears just as the speedo hits 12mph.
 

Woosh

Trade Member
May 19, 2012
15,846
13,493
Southend on Sea
wooshbikes.co.uk
did you get error 22 in pedaling mode?
 

Advertisers