Try declaring it at the top so something like thisGetting "'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.Getting "'pulseonlenght' was not declared in this scope" i know raj fixed it in his code with another one but what is the fix?
Any error numbers? Or are we free of error numbers?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.
Agreed. We should concentrate to get the pedalling code right.So it’s really just down to fine tuning the code on the pedals?
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.Woosh, do we require more testing with the picoscope?
I'll see what i can do, i'm busy today but when ever i got spare time i will get to it.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.
part of the reason is the bike is not on the road, the motor needs little power to reach full speed.It's because the slightest signal sent from the throttle will make the arduino go full power.
// 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.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 } } } }
thank you but you don't have to rush.I will give you the waveforms tonight even if I come home at midnight.