diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 83ba317..1d76fb5 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -20,6 +20,7 @@ add_subdirectory(buzzer) add_subdirectory(chassis) +add_subdirectory(communication) add_subdirectory(dbus) add_subdirectory(eigen) add_subdirectory(gimbal) diff --git a/examples/communication/CMakeLists.txt b/examples/communication/CMakeLists.txt new file mode 100644 index 0000000..fde216c --- /dev/null +++ b/examples/communication/CMakeLists.txt @@ -0,0 +1,26 @@ +# ---------------------------------------------------------------------- # +# # +# Copyright (C) 2022 # +# Illini RoboMaster @ University of Illinois at Urbana-Champaign. # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see . # +# # +# ---------------------------------------------------------------------- # + +project(example_communication ASM C CXX) + +irm_add_arm_executable(${PROJECT_NAME} + TARGET DJI_Board_TypeA + SOURCES main.cc) + diff --git a/examples/communication/main.cc b/examples/communication/main.cc new file mode 100644 index 0000000..f429806 --- /dev/null +++ b/examples/communication/main.cc @@ -0,0 +1,107 @@ +/**************************************************************************** + * * + * Copyright (C) 2022 RoboMaster. * + * Illini RoboMaster @ University of Illinois at Urbana-Champaign * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + * * + ****************************************************************************/ + +#include "bsp_gpio.h" +#include "bsp_print.h" +#include "bsp_uart.h" +#include "cmsis_os.h" +#include "main.h" +#include "protocol.h" + +#define RX_SIGNAL (1 << 0) + +extern osThreadId_t defaultTaskHandle; + +const osThreadAttr_t clientTaskAttribute = {.name = "clientTask", + .attr_bits = osThreadDetached, + .cb_mem = nullptr, + .cb_size = 0, + .stack_mem = nullptr, + .stack_size = 128 * 4, + .priority = (osPriority_t)osPriorityNormal, + .tz_module = 0, + .reserved = 0}; +osThreadId_t clientTaskHandle; + +class CustomUART : public bsp::UART { + public: + using bsp::UART::UART; + + protected: + /* notify application when rx data is pending read */ + void RxCompleteCallback() final { osThreadFlagsSet(clientTaskHandle, RX_SIGNAL); } +}; + +static communication::Host* host = nullptr; +static CustomUART* host_uart = nullptr; +static bsp::GPIO *gpio_red, *gpio_green; + +void clientTask(void* arg) { + UNUSED(arg); + uint32_t length; + uint8_t* data; + + while (true) { + /* wait until rx data is available */ + uint32_t flags = osThreadFlagsWait(RX_SIGNAL, osFlagsWaitAll, osWaitForever); + if (flags & RX_SIGNAL) { // unnecessary check + /* time the non-blocking rx / tx calls (should be <= 1 osTick) */ + length = host_uart->Read(&data); + host->Receive(communication::package_t{data, (int)length}); + gpio_green->Low(); + osDelay(200); + gpio_green->High(); + } + } +} + +void RM_RTOS_Init(void) { + print_use_uart(&huart8); + + host_uart = new CustomUART(&huart6); + host_uart->SetupRx(300); + host_uart->SetupTx(300); + + host = new communication::Host; + + gpio_red = new bsp::GPIO(LED_RED_GPIO_Port, LED_RED_Pin); + gpio_green = new bsp::GPIO(LED_GREEN_GPIO_Port, LED_GREEN_Pin); + gpio_red->High(); + gpio_green->High(); +} + +void RM_RTOS_Threads_Init(void) { + clientTaskHandle = osThreadNew(clientTask, nullptr, &clientTaskAttribute); +} + +void RM_RTOS_Default_Task(const void* argument) { + UNUSED(argument); + + while (true) { + set_cursor(0, 0); + clear_screen(); + print("%c\n", host->pack.chars[0]); + //print("%s\n", host->target_angle.pitch); + //print("%s\n", host->target_angle.yaw); + //print("%s\n", host->no_target_flag.dummy); + //print("%s\n", host->shoot_cmd.dummy); + osDelay(500); + } +} diff --git a/vehicles/Sentry/rm_rtos.cc b/vehicles/Sentry/rm_rtos.cc index e922734..9cc640b 100644 --- a/vehicles/Sentry/rm_rtos.cc +++ b/vehicles/Sentry/rm_rtos.cc @@ -107,8 +107,8 @@ void RM_RTOS_Default_Task(const void* args) { UNUSED(args); control::MotorCANBase* motors[] = {pitch_motor, yaw_motor, load_motor}; control::gimbal_data_t gimbal_data = gimbal->GetData(); - bsp::GPIO laser(LASER_GPIO_Port, LASER_Pin); - laser.High(); + //bsp::GPIO laser(LASER_GPIO_Port, LASER_Pin); + //laser.High(); bool load = false; bool abs_mode = true;