marce002
100 W
is a Bitwise XOR? what library should be included?
Last edited:
/*
* Original code from trufinv. See:
* https://www.endless-sphere.com/sphere/threads/using-fardrivers-lin-protocol.120345/#post-1805274
* https://www.endless-sphere.com/sphere/threads/votol-serial-communication-protocol.112970/page-2#post-1805276
*/
const unsigned int SIF_PIN = 2;
short battery = 0;
short current = 0;
short currentPercent = 0;
short rpm = 0;
long faultCode = 0;
bool regen = false;
bool brake = false;
unsigned long lastTime;
unsigned long lastDuration = 0;
byte lastCrc = 0;
byte data[12];
int bitIndex = -1;
void setup()
{
Serial.begin(115200);
Serial.println("Begin.");
pinMode(SIF_PIN, INPUT);
lastTime = micros();
attachInterrupt(digitalPinToInterrupt(SIF_PIN), sifChange, CHANGE);
}
void loop()
{
// put your main code here, to run repeatedly:
}
void sifChange()
{
int val = digitalRead(SIF_PIN);
unsigned long duration = micros() - lastTime;
lastTime = micros();
if (val == LOW)
{
if (lastDuration > 0)
{
bool bitComplete = false;
float ratio = float(lastDuration) / float(duration);
if (round(lastDuration / duration) >= 31)
{
bitIndex = 0;
}
else if (ratio > 1.5)
{
// bit value 0
bitClear(data[bitIndex / 8], 7 - (bitIndex % 8));
bitComplete = true;
}
else if (1 / ratio > 1.5)
{
// bit value 0
bitSet(data[bitIndex / 8], 7 - (bitIndex % 8));
bitComplete = true;
}
else
{
Serial.println(String(duration) + "-" + String(lastDuration));
}
if (bitComplete)
{
bitIndex++;
if (bitIndex == 96)
{
bitIndex = 0;
byte crc = 0;
for (int i = 0; i < 11; i++)
{
crc ^= data[i];
}
if (crc != data[11])
{
Serial.println("CRC FAILURE: " + String(crc) + "-" + String(data[11]));
}
if (crc == data[11] && crc != lastCrc)
{
lastCrc = crc;
for (int i = 0; i < 12; i++)
{
Serial.print(data[i], HEX);
Serial.print(" ");
}
Serial.println();
battery = data[9];
battery = data[9];
current = data[6];
currentPercent = data[10];
rpm = ((data[7] << 8) + data[8]); // * 1.91; TODO: Change ratio to match wheel-size/motor gearing.
brake = bitRead(data[4], 5);
regen = bitRead(data[4], 3);
Serial.print("Battery %: " + String(battery));
Serial.print(" Current %: " + String(currentPercent));
Serial.print(" Current A: " + String(current));
Serial.print(" RPM: " + String(rpm));
if (brake) Serial.print(" BRAKE");
if (regen) Serial.print(" REGEN");
Serial.println();
}
}
}
}
}
lastDuration = duration;
}
had issues displaying the data on an external display also. Fixed by using usb cable with shielding for data transfer. Also instead of checking like this: "if (ratio > 1.5)", you could just compare the values like lastDuration > duration. It seems to provide more accurate data. Also avoid any kind of float operations in the interrupt if you're using ESP32.Oh yes, the EMI is through the roof here. Kind of hard to collect reliable data for a Odometer.
I'm not familiar with Teraterm, so can't read the log-output as it is.attached is my teraterm log, it seems to be receiving data, can anyone tell me if its the same data you would expect?
LOGGED SESION IS: turned on, accelerated and brake then turned off.
Can you share what your current setup is? I'd love to try this on my FarDriver and have a nicer external display. Thanks!had issues displaying the data on an external display also. Fixed by using usb cable with shielding for data transfer. Also instead of checking like this: "if (ratio > 1.5)", you could just compare the values like lastDuration > duration. It seems to provide more accurate data. Also avoid any kind of float operations in the interrupt if you're using ESP32.
Well I have 3 of them, one ebike and 2 electric motorcycles. The displays are 0.96" SSD1306, 2" ST7789V and 3.2" ILI9341. The 0.96" is in a simple voltmeter/ignition enclosure and I use the KT display buttons to scroll between screens, for the other 2 I've designed and 3d printed enclossures (they also have a button on the side to scroll between screens). All are driven with esp32sCan you share what your current setup is? I'd love to try this on my FarDriver and have a nicer external display. Thanks!
Finally received my new oscilloscope and did some measurements.I see some possibilities to improve trufinv's excellent proof-of-concept.
- sync on >900 Tosc Low, followed by 32 Tosc High.
- filter EMI where period is below 32 Tosc.
- filter EMI where period is above 64 Tosc.
@trufinv With 32us <Tosc <320us, Can you clarify wether Tosc is a constant time of 1/9600 (104 microseconds) or 32*Tosc ?
i actually have it dual mode. most of the time I get my data (voltage included) via serial. when i connect my phone or laptop to the esp32 via bloetooth serial then I use the sif (display output) and forward the serial packages from the phone/pc to the votol so it can be programmedhello #TRUFINV
I'm trying to display Battery Volt like your picture in the OLED 0.96 display, but i fin only 0 in that byte.... RPM, brake, and current seems to be working fine, but cant find any data for battery (despite that Volt is not present in the datasheet you were given) that is why I ask you how did you determine Volts in your OLDE picture port #69
my controller is 96v EM100 , OCT 2024 , maybe they changed something?
fardrivers allow you to customize the sif packets up to a point whereas votols don't. however for me the votol is better as I can use the serial protocol that is well documented and works great and provides better data than the very limited sif packages.But having a votol em100 and a proper connection to one line wire, my stm32 device is not showing up any data related to BATTERY V or BAT %, like if the datasheet where outdated beause come as 0 no data for battery, even less for VOLTS.... all the rest is fine like REGEN, brake, RPM and so on.... do you know what can it be? i mean nothing here:
battery = data[9];