Lab 2: Bluetooth Comm
Objectives:
-
The purpose of this lab is to establish communication between our computer and the Artemis board through the bluetooth stack.
We will be using Python inside a Jupyter notebook on the computer end and the Arduino programming language on the Artemis side.
Materials
- Artemis Board
- PC or Laptop:
- Windows OS Minimum Requirement: Windows 10
- Mac OS Miinimum Requirement: Mac OS 12.0
- Linux/Free BSD Kernal Requirements: Linux kernel 4.15+ and bluez 5.48+
- Windows OS Minimum Requirement: Windows 10
- Mac OS Miinimum Requirement: Mac OS 12.0
- Linux/Free BSD Kernal Requirements: Linux kernel 4.15+ and bluez 5.48+
Setup
In this lab we will be leveraging the Arduino BLE library BLE to execute the bluetooth communication protocols betwween the Artemis board and the computer. The BLE library supports all the Arduino boards that have the hardware enabled for BLE and Bluetooth 4.0 and above and it's meant for low power operation as well as low data rate transmission.
Computer Setup
Below are the required computer set up for this lab. These steps are to be done in a Command Line Interface
Install Python
Check the version of python on your computer by typing in the following commands on your CLI. If it's not installed then go ahead download the latest version of python on your computer
Virtual Environment
After downloading python, go ahead and set up a virtual environment by executing the following command
Navigate to your project directory and then type the following command to create a virtual Python installation in your project directory
Activate the virtual environment with following command. In this case my project directory is ece4960.
Whenever you're ready to exit the environment, simply type the following command to leave
Package Install
Note: Make sure your Pip package is installed is the most up-to-date version before continuing
Once you're set, activate the virtual environment and execute the following command to install the set of packages you need for the lab
After setting up our computer, we then downloaded and unziped a codebase into our project directory. The codebase was a set of Python and Artemis packages that provided a base code necessary to establish a communication channel between your computer and the Artemis board through BLE. We'll use them to setup an initial communication channel in this lab, and work towards a more comprehensive robot control protocol in your future lab sessions.
Jupyter Lab
We will be using Jupyter notebooks to write Python code. Before you can open a Jupyter notebook, you need to start the Jupyter server. If the Jupyter server is stopped, you will not be able to run/open/modify any Jupyter notebooks. We typed following command in the CLI to start the Jupyter server
Computer Set-up is done after this
Artemis Board Setup
We Installed ArduinoBLE from the library manager in the Arduino IDE and flashed the artemis board with the following sketch, ble_arduino.ino, which printed out the MAC address of the board on the serial monitor as shown below.
To communicate over bluetooth from the computer to ther artemis board, we used a demo jupyter notebook that was inlcuded in the previously downloaded codebase and executed commands to the board. See below for the demo notebook file that was used to establish communication link.
This is the output of the Serial monitor on the Artemis side when the SEND_TWO_INTS command was executed
Lab Tasks
Task 1: Echo
This task asks that we send an ECHO command with a string value from the computer to the Artemis board, and receive an augmented string on the computer. In order to get a response back from the artemis board, we must modify the code on the Arduino end send a response back when it receives a "ECHO" coommand. As dictated in the lab handout the form of the response had to be "Robot says -> Message :)." See below for the output of the echo command on Jupyter notebook.
Task 2: Send Three Floats
Here we are asked to send three floats to the Artemis board using the SEND_THREE_FLOATS command and extract the three float values in the Arduino sketch. Displayed below is the response from the serial monitor as well as the command from Jupyter Notebook
Task 3: Notification Handler
In this exercise, we set up a notification handler in Python to receive the characteristic float value from the Artemis board. As indicated, in the above demo notebook, there is a BLE member function called receive_float() which essentially extracts the value once. However, in this task we use a notification handler supported by the BLE member function start_notify() to update a global variable with the characteristic value everytime it changes so that way we don't have to explicitly call the receive_float() function and all we need to do is call the print function of the global variable to extract the float value. Note that this is all achieved from the python end and nothing was modified on the Artemis side.
To achieve this semi-automatic updating, we first define a global variable meant for storing the float, then we define a callback function that takes two parameter —a uuid string object and a bytearray of the float— which essentially handles the processing/conversion of the byte array into a human readable float form. We then make a call to the start_notify() function and pass in the uuid of interest and the callback function we previously created as the first and second arguments respectively to the notify function. With this done we've essentially created an automatic float updater.