void print_SBus(){
uint16_t channels[16]; bool failSafe; bool lostFrame;
while (x8r.read(&channels[0], &failSafe, &lostFrame)) {
for (unsigned int i = 0; i < 4; i++) {
dataFile.print(channels[i]);
dataFile.print(",");
}
This is a subroutine intended to store the first four channels of a 16 channel S-Bus packet to a micro-SD file. It runs at the end of an output routine in the larger application which writes gps, and myriad sensor data to the microSD.
There is a synchronization issue here. The overall code runs at about 10Hz, that is it reads every sensor at approximately this frequency affected somewhat by variation in data size from some sensors.
The S-Bus data stream runs at a different frequency - much faster.
The problem with the snippet above is that while it always reads the first four channels of each packet, it often reads the next packet at well and so I get two instances instead of the one I need.
I'm assuming the problem is the "WHILE" loop sometime doesn't shut off quickly enough after reading the first packet, and if the next one it sees is a channels[0] packet, it reads that one as well.
I tried using an "IF" statement instead, but it often missed the channels[0] flag and accordingly failed to record that packet - a miss in other words. I have to have a read every time this snippet runs.
So my question is can I add another line to this to write only one packet of the four channels? Alternatively, is there another way to run this little loop which will assure saving only a single instance of the data?
MorganS.
I liked the part where the problem is in the code I didn't post. All of this is pretty big, but I'd like to work my way up part by part using the parts related to the question which will run independently. I'll also reference the library. This will take some time. I've been working on this off and on for 6 months so better for me not to be careless with how I present this.
The best way is to trim your code down to the shortest possible example which demonstrates the problem. Usually that will show you the problem before you post it here.
MorganS:
The best way is to trim your code down to the shortest possible example which demonstrates the problem. Usually that will show you the problem before you post it here.
good idea, but it's a little tough to do if the problem is driven by two asynchronous systems, one my data-recording method and the other the S-Bus cyclic frequency.
I'm Using Brian Taylor's SBUS library. It works fine without the duplication in a simple sketch where I'm only copying SBUs output to an SD.
problem could very well be an anomaly in the Sbus library. I'm in contact with Brian Taylor on this.
This did not turn out to be simple. I must have been lucky to get the SD routine to write the S-Bus packets to the Micro-SD at all. It is easy enough to trim the extra data from my CSV output files. This is more an aesthetic problem since I already need to trim other interesting but not always necessary data from these datasets. Accordingly, this doesn't represent any real problem.
I was unable to get a sketch which I thought simple enough to work at all. the S-Bus routine is repackaging packets which it has read from the S-Bus at 100,000 bps. These in turn must be sent to the SD card at some unknown speed. It could be that I need to flush the buffer between writes, but ....
I may eventually come back to this, build a small setup just to work this out, but for now, I'm going to drop it.
I apologize for any time anyone invested in this question.