Tags:
create new tag
view all tags

A cookbook recipe to make the Bluetooth protocol work

Introduction

Since you seem to have problems with point 1-3 of the development process (think and design) let me try to give you a cookbook recipe on how to get the protocol to work. This should have come out of the first part in the development process: a strategy on how to tackle the problem. I have not tried the recipe and I don’t know if the cake is going to taste well but please follow the recipe point by point.

If you think that something is unclear, missing or plain wrong, then please tell me such that we can discuss things

The recipe:

  • In your folder, in which you have your Arduino sketches for the Bluetooth, create 3 new folders:

    • protocol

      • receiver

      • transmitter

    • protocolTest

      • receiver

      • transmitter

    • doc

Basic protocol string transmission

  • Put your “Hello World” program into protocolTest, the transmitting part into transmitter and the receiving part into receiver and make it work again

  • Document this step:

    • What are the connections between Arduino and the Bluetooth module?

    • Which serial ports do you use for what (standard serial, soft serial)?

    • Which Bluetooth module (HC-05 or HC-06) do you have on which side? Etc…

  • Replace “Hello World” by:
    Serial.println(“l:0x _ _ _ ,r:0x _ _ _ ,f:0x _ _ _ ,b:0x _ _ _ ,c:0x _ _ _ “);
    (replace the “_” with spaces,
    depending on how you cabled your hardware Serial.println may have to be replaced by
    softSerial.println(“l:0x _ _ _ ,r:0x _ _ _ ,f:0x _ _ _ ,b:0x _ _ _ ,c:0x _ _ _ “);

  • Now create a character buffer with the text above:
    char *protocolString[]= “l:0x _ _ _ ,r:0x _ _ _ ,f:0x _ _ _ ,b:0x _ _ _ ,c:0x _ _ _ “
    Serial.println(protocolString);

  • If this works, document it!

Conversion form hex to string

  • Forget about Bluetooth for the moment and write a function that converts a 3 digit hex number into a string of 3 characters:
    0x3fa → “3fa”
    This is the function prototype: int (hex2asc(unsigned short value, char *valueString);
    The function returns 0 on success, -1 on failure (the number is not a 10 digit value)
    Write a simple main program to test this function. This may be an arduino sketch where you have the function and the main program calls this function with whatever value you decide to try. Check that bad numbers (e.g. 0xfff) are refused with an error code.

  • Write the inverse function short asc2hex(char *numString) which takes the string of a 3 digit hex number and converts it back into a number. On success the calculated number is returned, on failure you return -1;

  • Test both routines with Arduino sketches

  • When they are working, copy the Arduino code to the protocol folder and document the functions on the doc folder

Inserting the converted number into the protocol string

  • Create an Arduino sketch which simply prints the protocol string on the serial console

  • Insert a character into the buffer at the position of the first space: protocolString[4] and print the string on the serial console again

  • Insert the 3 characters coming back from hex2asc into the first 3 spaces and print the string again.

  • Fill all 5 positions with arbitrary numbers and print the string again.

The checksum

  • Write a function unsigned char checksum(char *protocolString) that adds the values of each character in the string up to the “0x” after “c:”.
    You can use a while loop and check for the end of string character ‘\0’ character to get out of the loop, or you can use a for loop where the index runs from 0 to strlen(protocolString);
    The function returns the lowest significant 10 bits of the sum.

  • Write an Arduino sketch to test the checksum function.

  • Once the function is working, you copy it to the protocol folder and you document it in the doc folder.

Reading the joystick

  • Write a function int joystick(unsigned short *leftRight, unsigned short *forAndBackward) which reads the joystick values.

  • Write an Arduino sketch to test it.

  • Write a second function
    int motorValues(
    unsigned short leftRight,
    unsigned short forAndBackward,
    unsigned short *motorMovement,

    converting the raw joystick values into values usable for the movement.
    motorMovement is an array with index forward,backward,left,right

  • Write an Arduino sketch to test reading of joystick values combined with the conversion

  • Once the function work as expected, copy them to the protocol folder and document them in the doc folder

Putting things together

  • Take the protocol string, fill arbitrary (but valid) number strings into the slots, where we had the spaces and send this string to the receiving Arduino.
    Print the string on the receiving side

  • Now, on the transmitting side, read the joystick values with the function you have written before, convert them to movement values (with the function you have written and tested before), convert these numbers to strings with hex2asc and fill in the slots in the protocol.

  • Send this filled protocol string to the other side and print it

  • If it works, document it!

  • Now, take the protocol string with the movement values filled in and pass it to the checksum function. Fill in the checksum value (after having converted it to a string with hex2asc) and send the completed protocol string to the receiving side, where you print it

  • Document everything, once it is working.

The final result

  • On the receiving end: Calculate the checksum with the previously tested checksum function

  • Extract the checksum string from the protocol and convert it to a number

  • Compare the calculated checksum with the one extracted from the protocol. Print an error message if they do not coincide

  • Extract the 4 movement value strings, convert them to binary values with asc2hex and print them

-- Uli Raich - 2017-11-21

Comments

Topic revision: r1 - 2017-11-21 - uli
 
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback