Again, I've run into another roadblock in the course of this project.
First of all: the communications glitch has been solved. Here's the technical lowdown.
My code has a simple communications protocol:
- Send a "~" to signal the start of a packet
- Send a byte for the analog joystick's X position
- Send a byte for the analog joystick's Y position
- Send a byte for the status of the left digital trigger
- Send a byte for the status of the right digital trigger
I know, I know, the two trigger bytes can be merged for speed. I'm working on that. For now, I just want communications to work.
This is implemented fairly easily. The bug, however, came from poor planning. I put some simple code in the receiver to wait for data to arrive in the serial buffer before beginning to process a new packet. Once data arrives, the code checks that the first byte is a tilda ("~"). If so, then it reads four bytes, processes them, and sends the data out to the motors. If the first byte isn't a tilda, it will keep checking each incoming byte until it gets a tilda. See the problem?
The bug came from my code waiting for data to arrive in the serial buffer. I was waiting for any amount of data, no matter how big or small. In doing so, I overlooked this fact: just because data has arrived doesn't mean the entire packet has finished transmitting. Because of this, the code was trying to process an incomplete data packet. The processing code would finish before the packet completely arrived, returning a -1 (no data) code for each "ghost" byte. Once it finished, the loop would see more data in the buffer: the rest of the broken packet. The tilda ("~") check would fail, and the loop would keep checking incoming bytes until it found the start of the next packet. There still wouldn't be a full packet in the buffer, so the cycle would continue, breaking the communications scheme.
The fix: The buffer now waits for at least 5 bytes: a full packet. Simple, huh?
Before this code update, I also had a newline byte sent to mark the end of a packet. With the XBee's onboard redundancy and the tilda already there, I found this to be a waste of time, so I removed it. Instant speed boost!
However, with every fix comes another gotcha. In this case, it was a hardware problem. When testing the new code, I found that the brushless motor wouldn't turn. Instead, it jittered in place.
Quick side note on how brushless motors of the type I use are driven:
The motor has three sets of electromagnetic coils that turn the shaft. They must be energized in the right order in order for the motor to turn. This is handled by a brushless ESC (electronic speed controller). This is driven in the exact same way as a servo motor, but 0 degrees (on a normal servo) corresponds to "off", and 180 degrees corresponds to "full throttle". Since this ESC has a built in BEC (battery elimination circuit), it also connects to the Li-Po battery and regulates +5 volts to power the receiver.
I believe that the motor's coils were being energized in the wrong order, causing the jittering back and forth. When I tried to diagnose it, I first tested my receiver (the most custom thing in the system) with the basic Arduino "sweep" test program. It produced the same symptoms and a problem: at full power, the motor began to smoke. I'm not sure why this happened, but it seems to have partially broken the motor. The startup beeps (generated by the ESC and produced by the motor vibrating) are very quiet, when they should be quite loud. I'm not sure where the original fault came from: the ESC improperly driving the motor, or the motor itself. The funny thing is, it was working fine when I had to use the setup for a completely separate demonstration. The only difference was the receiver/controller: I was driving it off of an Arduino UNO. That shouldn't affect the setup, though. Right?
So, as of now, I'm stuck. I think the next step will be to see if the ESC or the motor needs to be replaced, and if so, which one (or both). I'm curious to know why the motor started jittering, since the same setup I used worked in the past. Does anybody know out there?
Note: I also made some progress on the boat assembly. I'll post about that soon.