Cleric-K / vJoySerialFeeder

Feed Virtual Joystick driver with data from a serial port
GNU General Public License v3.0
252 stars 55 forks source link

Failsafe issue (other threads don't have the solution :( ) #53

Closed rdelvax closed 2 years ago

rdelvax commented 2 years ago

Hi, I'm using the latest serialfeeder, and I'm sending ibus data over USB. (Wemos D1 mini.) When checking in serial monitor I get : 12 40 1F 02 1F 02 1F 02 1F 02 1F 02 1F 02 1F 02 1F 02 A5 FE I'm sending 8 channels, all of them fixed value 543. I use a fixed value to make sure it's not a problem with the A/D code. When I did use the complete code, it showed the measured data.

vjoySerialFeeder states: Connected but ALWAYS with failsafe. Sometimes waiting for serial data, sometimed serial data read timeout. Also, frefresh interval is always around 100ms, even when I change the interval in my code. Even with 1s! interval in the code, the status claims a 100ms refresh!

This is my code: (using the ibus.cpp and ibus.h from the original.) Wemos only has 1 analog port, I use a 16ch. analog multiplexer to read all values (even switches...) Values are read correctly (I verified)


include

include "ibus.h"

define UPDATE_INTERVAL 10 // milliseconds

define MAX_CHANNEL 8 // Number of TX channels

define NUM_CHANNEL (MAX_CHANNEL - 1)

int DataBlock[MAX_CHANNEL]; IBus ibus(NUM_CHANNEL);

void setChannel(int Ch) { digitalWrite(D1, Ch & 0x01); digitalWrite(D2, Ch & 0x02); digitalWrite(D3, Ch & 0x04); digitalWrite(D4, Ch & 0x08); }

void readInputs(void) { for(int i=0; i < MAX_CHANNEL; i++) { setChannel(i); digitalWrite(D5, 0); analogRead(A0); DataBlock[i]=analogRead(A0); digitalWrite(D5, 1); } }

void transmitData(void) { ibus.begin(); for(int i=0; i<=NUM_CHANNEL; i++) { ibus.write(DataBlock[i]); // ibus.write(543); } ibus.end(); }

void setup() { // put your setup code here, to run once: pinMode(D1, OUTPUT); pinMode(D2, OUTPUT); pinMode(D3, OUTPUT); pinMode(D4, OUTPUT); pinMode(D5, OUTPUT); digitalWrite(D5, 1); // LOW is analog mux enabled!!! Serial.begin(115200); readInputs(); }

void loop() { unsigned long time = millis(); // put your main code here, to run repeatedly: readInputs(); transmitData(); time = millis() - time; // time elapsed in reading the inputs if(time < UPDATE_INTERVAL) // sleep till it is time for the next update delay(UPDATE_INTERVAL - time); }

Cleric-K commented 2 years ago

I think the problem comes from IBus ibus(NUM_CHANNEL); you should create the object with argument 8. Currently you're giving it 7. This sets the IBUS frame to be 18 bytes long: 7 channels 2 bytes each (14 bytes) + 2 header bytes in the beginning + 2 bytes bytes at the end for checksum. Yet in the loop you actually send all 8 channels, thus at the receiving end, where you send the 8th channel vjoysf expect the checksum.

You should change NUM_CHANNEL to MAX_CHANNEL and it should work. Also, hit the Setup of the IBUS protocol and make sure to check the use 16bit channel values. This is not strictly necessary.

rdelvax commented 2 years ago

Thank you so much!! I copied the example I had for initializing. Apparently I overlooked this obvious mistake. It works ! Thank you very much!