Merge pull request #9 from Block-Party-VR/6-switch-over-to-using-freertos-tasks

6 switch over to using freertos tasks
This commit is contained in:
Quinn
2024-08-20 18:29:52 -04:00
committed by GitHub
8 changed files with 172 additions and 69 deletions

2
.gitignore vendored
View File

@@ -9,3 +9,5 @@ Schematics/Block-Party/Block-Party-Cube-Bottom-Board/production
Schematics/Block-Party/Block-Party-Main-Board/Block-Party-Main-Board-backups/ Schematics/Block-Party/Block-Party-Main-Board/Block-Party-Main-Board-backups/
Schematics/Block-Party/Block-Party-Cube-Bottom-Board/Block-Party-Bottom-Board-backups/ Schematics/Block-Party/Block-Party-Cube-Bottom-Board/Block-Party-Bottom-Board-backups/
Schematics/Block-Party/Block-Party-Cube-Top-Board/Block-Party-Cube-Top-Board-backups/ Schematics/Block-Party/Block-Party-Cube-Top-Board/Block-Party-Cube-Top-Board-backups/
platformio-local.ini

View File

@@ -15,6 +15,11 @@ When the physical user removes a block from the board, the corresponding digital
When the VR user hovers over or picks up a digital cube, the corresponding physical cube changes color to show the physical user what the VR user is doing. When the VR user hovers over or picks up a digital cube, the corresponding physical cube changes color to show the physical user what the VR user is doing.
## Setup ## Setup
### Repository Setup
When the repository is first cloned you will need to initialize all of its submodules.
In the terminal type: `git submodule update --init --recursive` which will initalize all of the submodules.
### Board Assemble ### Board Assemble
#### Parts List #### Parts List
- Foam core (scavenged) - Foam core (scavenged)
@@ -53,7 +58,7 @@ wiring diagram for the cube is in the `documentation` directory.
- Arduino libraries - Arduino libraries
### Bluetooth Module ### Bluetooth Module
On v0.1 of the baord, a seperate bluetooth module (HC-05) is being used for bluetooth. This module needs to be programmed when first plugged in. To program the module, disconnect it from power and hold the "EN" button on the module. (The button should be the only button on the HC-05 module). While still holding down the button, reconnect the module to the ESP32 and press the ESP32's reset button. Wait 5 seconds while still holding down the "EN" button on the module, then release the button, power cycle the module, and you're done. On v0.1 of the board, a seperate bluetooth module (HC-05) is being used for bluetooth. This module needs to be programmed when first plugged in. To program the module, disconnect it from power and hold the "EN" button on the module. (The button should be the only button on the HC-05 module). While still holding down the button, reconnect the module to the ESP32. Let go of the button and then press the ESP32's reset button. Wait 5 seconds while still holding down the "EN" button on the module, then release the button, power cycle the module, and you're done.
## Run ## Run
- Power up the board and pair it with the headset over bluetooth. - Power up the board and pair it with the headset over bluetooth.

13
include/COMMANDS.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
#include <cstdint>
/**
* @brief These are the serial commands that the board supports
*/
enum Commands : uint8_t{
BoardState = 0,
PING = 1,
SetStackColors = 2,
GoToIdle = 3
};

View File

@@ -0,0 +1,28 @@
/**
* @file GlobalPrint.h
* @brief a method of printing serial data to all outgoing communication methods
*/
#pragma once
#include <Arduino.h>
namespace GlobalPrint{
static void Print(const char * s){
Serial.print(s);
}
static void Print(const String &s){
GlobalPrint::Print(s.c_str());
}
static void Println(const char * s){
GlobalPrint::Print(s);
GlobalPrint::Print("\n");
}
static void Println(const String &s){
GlobalPrint::Println(s.c_str());
}
}

View File

@@ -9,6 +9,7 @@
; https://docs.platformio.org/page/projectconf.html ; https://docs.platformio.org/page/projectconf.html
[platformio] [platformio]
default_envs = esp32s3_release ; this ensures that only this environment is built for anything but the debug build default_envs = esp32s3_release ; this ensures that only this environment is built for anything but the debug build
extra_configs = platformio-local.ini
[env] [env]
; platform = espressif32 ; platform = espressif32
@@ -17,10 +18,9 @@ platform = https://github.com/platformio/platform-espressif32.git
board = esp32-s3-devkitc-1 board = esp32-s3-devkitc-1
framework = arduino framework = arduino
build_flags = -Iinclude build_flags = -Iinclude
; lib_ldf_mode = chain+
monitor_speed = 9600 monitor_speed = 9600
monitor_filters = esp32_exception_decoder, colorize, send_on_enter monitor_filters = colorize, send_on_enter
upload_speed = 2000000 ;ESP32S3 USB-Serial Converter maximum 2000000bps upload_speed = 2000000 ;ESP32S3 USB-Serial Converter maximum 2000000bps
lib_deps = adafruit/Adafruit NeoPixel@^1.12.0 lib_deps = adafruit/Adafruit NeoPixel@^1.12.0
@@ -35,4 +35,6 @@ build_type = debug
debug_speed = 20000 debug_speed = 20000
; debug_port = COM7 ; debug_port = COM7
; monitor_port = COM14 ; monitor_port = COM14
build_flags = -O1 -Iinclude build_flags =
-D DEBUG = 1
-I include

View File

@@ -1,35 +1,34 @@
// Other peoples libraries // Other peoples libraries
#include <Arduino.h> #include <Arduino.h>
#include <BluetoothSerial.h> #include <BluetoothSerial.h>
#include <FreeRTOS.h>
// Static Defines
#include "PINOUT.h"
#include "BOARD-DEFINITIONS.h"
#include "COMMANDS.h"
// project specific libraries // project specific libraries
#include "BluetoothSerial.h" #include "BluetoothSerial.h"
#include "SerialMessage.h" #include "SerialMessage.h"
#include "BoardLayout.h" #include "BoardLayout.h"
#include "BOARD-DEFINITIONS.h"
#include "Color.h" #include "Color.h"
#include "ColorManager.h" #include "ColorManager.h"
#include "GlobalPrint.h"
// --------------------------------------------------
// ----------------- Types ----------------------
// --------------------------------------------------
enum Commands : uint8_t{
BoardState = 0,
PING = 1,
SetStackColors = 2,
GoToIdle = 3
};
// -------------------------------------------------- // --------------------------------------------------
// ----------------- VARIABLES ---------------------- // ----------------- VARIABLES ----------------------
// -------------------------------------------------- // --------------------------------------------------
TaskHandle_t updateCommunicaitonTask;
TaskHandle_t updateBoardTask;
uint32_t boardStateTimer{0}; uint32_t boardStateTimer{0};
bool boardStateHasChanged{false}; bool boardStateHasChanged{false};
uint32_t boardStateMaxUpdatePeriod{34}; // this is a little slower than 30fps uint32_t boardStateMaxUpdatePeriod{34}; // this is a little slower than 30fps
// BluetoothSerial SerialBT; // BluetoothSerial SerialBT;
// BluetoothSerialMessage serialMessageBT(&SerialBT); // BluetoothSerialMessage serialMessageBT(&SerialBT);
SerialMessage<500> serialMessage(&Serial); SerialMessage<500, 10> serialMessage(&Serial);
BoardLayout board(BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT, stacks); BoardLayout board(BOARD_WIDTH, BOARD_LENGTH, BOARD_HEIGHT, stacks);
// Temporary thing until we can get bluetooth color management working on the quest // Temporary thing until we can get bluetooth color management working on the quest
@@ -38,6 +37,11 @@ ColorManager colorManager(&board);
// -------------------------------------------------- // --------------------------------------------------
// ----------------- FUNCTIONS ---------------------- // ----------------- FUNCTIONS ----------------------
// -------------------------------------------------- // --------------------------------------------------
/**
* @brief Send programming commands to the serial to bluetooth adapter so
* it is set up as expected for the VR headset
* @post the serial baud rate will be set to 9600
*/
void SetupBluetoothModule(){ void SetupBluetoothModule(){
Serial.begin(38400); Serial.begin(38400);
Serial.print("AT+UART=9600,0,0\r\n"); // set baud rate to 9600 Serial.print("AT+UART=9600,0,0\r\n"); // set baud rate to 9600
@@ -52,37 +56,33 @@ void SetupBluetoothModule(){
Serial.print("AT+ROLE=0\r\n"); // set to slave Serial.print("AT+ROLE=0\r\n"); // set to slave
delay(100); delay(100);
// exit at mode and go into pairing mode // exit at mode and go into pairing mode
Serial.print("AT+INIT\r\n"); Serial.print("AT+INIT\r\n");
Serial.begin(9600); Serial.begin(9600);
delay(100); delay(100);
} }
void printBoardState(){ void printBoardState(){
// create a buffer to hold the board state // create a buffer to hold the board state
uint16_t boardState[BOARD_WIDTH * BOARD_LENGTH]; uint16_t boardState[BOARD_WIDTH * BOARD_LENGTH];
// read in the board state // read in the board state
board.GetBoardState(boardState); board.GetBoardState(boardState);
Serial.print("!0,"); GlobalPrint::Print("!0,");
// SerialBT.print("!0,");
for(int i = 0; i < (BOARD_WIDTH * BOARD_LENGTH); i++){ for(int i = 0; i < (BOARD_WIDTH * BOARD_LENGTH); i++){
Serial.print(boardState[i]); GlobalPrint::Print(String(boardState[i]));
// SerialBT.print(boardState[i]);
if(i == (BOARD_WIDTH * BOARD_LENGTH) - 1){ if(i == (BOARD_WIDTH * BOARD_LENGTH) - 1){
break; break;
} }
Serial.print(","); GlobalPrint::Print(",");
// SerialBT.print(",");
} }
Serial.println(";"); GlobalPrint::Println(";");
// SerialBT.println(";");
} }
void setStackColor(uint32_t * args, int argsLength){ void SetStackColor(uint32_t * args, int argsLength){
uint32_t stackNum = args[1]; uint32_t stackNum = args[1];
uint32_t numColors = (argsLength - 2) / 3; uint32_t numColors = (argsLength - 2) / 3;
Color colors[numColors]; Color colors[numColors];
@@ -95,60 +95,62 @@ void setStackColor(uint32_t * args, int argsLength){
} }
board.SetStackColors(stackNum, colors); board.SetStackColors(stackNum, colors);
} }
void parseData(uint32_t * args, int argsLength){ void parseData(Message<500, 10> &message){
int32_t * args{message.GetArgs()};
uint32_t argsLength{message.GetArgsLength()};
uint32_t command = args[0]; uint32_t command = args[0];
switch(command){ switch(command){
case Commands::BoardState: case Commands::BoardState:
printBoardState(); printBoardState();
break; break;
case Commands::PING: case Commands::PING:
Serial.print("!"); GlobalPrint::Println("!" + String(Commands::PING) + ";");
// SerialBT.print("!");
Serial.print(Commands::PING);
// SerialBT.print(Commands::PING);
Serial.println(";");
// SerialBT.println(";");
break; break;
case Commands::SetStackColors: case Commands::SetStackColors:
Serial.println("!2;"); GlobalPrint::Println("!2;");
// SerialBT.println("!2;");
colorManager.Enable(false); colorManager.Enable(false);
setStackColor(args, argsLength); SetStackColor(reinterpret_cast<uint32_t *>(args), argsLength);
break; break;
case Commands::GoToIdle: case Commands::GoToIdle:
Serial.println("!3;"); GlobalPrint::Println("!3;");
// SerialBT.println("!3;");
colorManager.Enable(true); colorManager.Enable(true);
break; break;
default: default:
Serial.println("INVALID COMMAND"); GlobalPrint::Println("INVALID COMMAND");
// SerialBT.println("INVALID COMMAND");
break; break;
} }
// now that we have run the command we can clear the data for the next command.
serialMessage.ClearNewData();
} }
// -------------------------------------------------- // --------------------------------------------------
// ----------------- SETUP AND LOOP ----------------- // ----------------- FREERTOS TASKS -----------------
// -------------------------------------------------- // --------------------------------------------------
void UpdateCommunication(void * params){
void setup() { Serial.println("Spawning UpdateCommunication task");
delay(1000); for(;;){
SetupBluetoothModule(); // DO serial processing
Serial.begin(9600); serialMessage.Update();
// SerialBT.begin("blockPartyBT"); if(serialMessage.IsNewData()){
Color colors[] = {Color(255, 0, 0), Color(0, 0, 0), Color(0, 0, 0)}; parseData(serialMessage);
board.SetStackColors(2, colors); }
// serialMessageBT.Update();
boardStateTimer = millis(); // if(serialMessageBT.IsNewData()){
// parseData(serialMessageBT.GetArgs(), serialMessageBT.GetArgsLength());
// serialMessageBT.ClearNewData();
// }
vTaskDelay(3);
}
Serial.println("UpdateCommunication task has ended unexpectedly!");
} }
void UpdateBoard(void * params){
void loop() { Serial.println("Spawning UpdateBoard task");
for(;;){
if(board.BoardStateHasChanged()){ if(board.BoardStateHasChanged()){
boardStateHasChanged = true; boardStateHasChanged = true;
} }
@@ -159,16 +161,44 @@ void loop() {
boardStateHasChanged = false; boardStateHasChanged = false;
} }
// DO serial processing
serialMessage.Update();
if(serialMessage.IsNewData()){
parseData(serialMessage.GetArgs(), serialMessage.GetArgsLength());
serialMessage.ClearNewData();
}
// serialMessageBT.Update();
// if(serialMessageBT.IsNewData()){
// parseData(serialMessageBT.GetArgs(), serialMessageBT.GetArgsLength());
// serialMessageBT.ClearNewData();
// }
colorManager.Update(); colorManager.Update();
vTaskDelay(5);
}
Serial.println("UpdateBoard task has ended unexpectedly!");
}
// --------------------------------------------------
// ----------------- SETUP AND LOOP -----------------
// --------------------------------------------------
void setup() {
// delay a little bit to get the serial monitor a chance to capture the next log messages.
delay(1000);
Serial.begin(9600);
Serial.println("Beginning Setup");
Serial.println("Configuring Bluetooth Adapter");
SetupBluetoothModule();
Serial.begin(9600);
Serial.println("Configuring communication methods");
serialMessage.Init(9600);
// SerialBT.begin("blockPartyBT");
xTaskCreate(UpdateCommunication, "UpdateCommunication", 10000, NULL, 0, &updateCommunicaitonTask);
Serial.println("Beginning Board Initializaiton");
xTaskCreate(UpdateBoard, "UpdateBoard", 10000, NULL, 0, &updateBoardTask);
Color colors[] = {Color(255, 0, 0), Color(0, 0, 0), Color(0, 0, 0)};
board.SetStackColors(2, colors);
boardStateTimer = millis();
Serial.println("Setup Complete");
}
void loop() {
// delete the loop task because we don't use it
vTaskDelete(NULL);
} }

View File

@@ -0,0 +1,23 @@
; This is a template that will allow you to add additional configuration
; options to the platformio.ini file without changing anything in git.
; options like upload/monitor ports are great things to put here
; because those will vary from computer to computer.
; To use this configuration, make a copy of this file and rename it to "platformio-local.ini"
; and then uncomment some of the following code or add your own options
; Feel free to modify the code as needed
; this will add additional configuration options to esp32s3_release environment.
; This environment is the default for building/sending code to the baord
; so this should be the first thing you change/modify
; Uncomment the following code as needed:
; [env:esp32s3_release]
; monitor_port = COM10
; upload_port = COM11
; monitor_speed = 115200
; this is the debug configuration which you probably don't need
; unless you're doing some complicated firmware development.
; [env:esp32s3_debug]
; debug_port = COM7
; monitor_port = COM14
; upload_port = COM12