I'm having trouble figuring out how to start my project. The objective is to control a robot with four servo motors and six DC motors through bluetooth. I have a bluetooth shield from Adafruit and I'm using the Arduino UNO. Ideally, I want to control this robot by sending it packets through Processing. The problem is I don't know how to create a packet with a header, payload, and checksum. I can't find any examples; can anyone help me find something to help me get started?
First, depending on the degree of reliability you need, you may not actually have to do this yourself, as the bluetooth implementation will already invisibly-to-you add, verify, and remove a packet and checksum framing around your data. But let's assume you want to add your own as well.
A packet is a batch of data to be transmitted as a contained unit. It may or may not contain exactly one useful message, but it is quite convenient if it does, and since you will be doing your own packetization (at this level) you can make sure that it does. So basically you will transmit something that has a structure, for example:
- Hi, I'm a packet
- attention HAL
- "Open the pod bay doors"
- The bytes in this message should add up to xx if you ignore the overflow
- That was a packet
There are countless ways you could implement this, but your life will be a lot easier if you make both the messages and the packet structure human readable, so that you can watch on the serial monitor, and perhaps even type manually. So let's say you did something like
For simplicity sake, instead of having a "Hi I'm a packet" header, we just use the newline character '\n' to mark the end of a packet, and assume that whatever comes next is the start of a new packet. Further, since bluetooth is already creating a one-to-one link, you don't need to address who you are talking to.
Now we have a message, for example "M1=200" meaning "drive motor 1 at PWM level 200" (out of 255).
I'm somewhat randomly choosing the '/' character to mark the division between the message body and it's checksum.
"BF" might be the low two hex digits of adding up all the previous bytes in this message (it's not, I just picked something random).
And finally you have your newline to mark the end of a message and the end of a packet, since in this scheme we've decided that 1 packet = 1 message.
For extra fun, if the robot itself is not dangerous, you can make a test mode where the receiver will accept messages with no '/' and checksum, and only check the checksum on those where it is present. This would allow you to manually type messages into a terminal when you are experimenting. Or you can make your own specialized terminal program which calculates and appends a checksum each time you press return
send serial data from PC. either use Arduino USB port or on real hardware serial port, needs USB to serial adaptor and a terminal software. (see diagram in below web)
for starting, no need for header. Just use simple text as defined yourself
on Arduino side, read serial input. For example, type letter 'a' on PC will blink LED, 'b' to move servo 30 degree
Replace serial communication (like RS-232 or a UART) with Bluetooth. Bluetooth supports many 'profiles', like headset, keyboard, etc. that are not suitable in your case.
See below video. You need to use the SPP profile. see https://learn.sparkfun.com/tutorials/bluetooth-basics/bluetooth-profiles
Make sure your purchased bluetooth device supports SPP. Some bluetooth device are permanently setup to do one fix action. However, many bluetooth need to be 'programmed' (told what to do) before doing the task via AT command. Set speed, pairing, etc.