mirror of
https://github.com/BotChain-Robots/firmware.git
synced 2026-07-08 17:47:21 +02:00
RMT integration
This commit is contained in:
@@ -9,7 +9,7 @@
|
||||
|
||||
#include "flatbuffers_generated/RobotModule_generated.h"
|
||||
|
||||
inline std::unordered_map<int, int> MODULE_TO_NUM_CHANNELS_MAP {{ModuleType_SPLITTER, 4}, {ModuleType_SERVO_1, 2}, {ModuleType_DC_MOTOR, 1}};
|
||||
inline std::unordered_map<int, int> MODULE_TO_NUM_CHANNELS_MAP {{ModuleType_SPLITTER, 4}, {ModuleType_SERVO_1, 2}, {ModuleType_DC_MOTOR, 1}, {ModuleType_SERVO_2, 2}};
|
||||
|
||||
#define PC_ADDR 0
|
||||
|
||||
@@ -22,4 +22,9 @@ inline std::unordered_map<int, int> MODULE_TO_NUM_CHANNELS_MAP {{ModuleType_SPLI
|
||||
#define DC_ENCODER_A 15
|
||||
#define DC_ENCODER_B 16
|
||||
|
||||
inline std::unordered_map<uint8_t, uint8_t> CHANNEL_TO_0_DEG_MAP{{0, 9} };
|
||||
inline std::unordered_map<uint8_t, uint8_t> CHANNEL_TO_90_DEG_MAP{{0, 7} };
|
||||
inline std::unordered_map<uint8_t, uint8_t> CHANNEL_TO_180_DEG_MAP{{0, 8} };
|
||||
inline std::unordered_map<uint8_t, uint8_t> CHANNEL_TO_270_DEG_MAP{{0, 10} };
|
||||
|
||||
#endif //MODULE_H
|
||||
|
||||
@@ -176,7 +176,7 @@ esp_err_t DataLinkManager::send(uint8_t dest_board, uint8_t* data, uint16_t data
|
||||
}
|
||||
};
|
||||
|
||||
// ESP_LOGI(DEBUG_LINK_TAG, "Sending %d bytes", offset);
|
||||
ESP_LOGI(DEBUG_LINK_TAG, "Sending %d bytes", offset);
|
||||
// print_buffer_binary(send_data, frame_size);
|
||||
|
||||
uint8_t channel_to_route = MAX_CHANNELS;
|
||||
@@ -192,7 +192,7 @@ esp_err_t DataLinkManager::send(uint8_t dest_board, uint8_t* data, uint16_t data
|
||||
ESP_LOGE(DEBUG_LINK_TAG, "Failed to find entry for %d", new_frame.receiver_id);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
// ESP_LOGI(DEBUG_LINK_TAG, "Sending message to %d", new_frame.receiver_id);
|
||||
ESP_LOGI(DEBUG_LINK_TAG, "Sending message to %d", new_frame.receiver_id);
|
||||
phys_comms->send(send_data, offset, &config, channel_to_route);
|
||||
}
|
||||
|
||||
@@ -271,6 +271,10 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (*recv_len < CONTROL_FRAME_OVERHEAD) {
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
uint8_t* message = (uint8_t*)pvPortMalloc(CONTROL_FRAME_OVERHEAD + MAX_CONTROL_DATA_LEN);
|
||||
if (message == nullptr){
|
||||
ESP_LOGE(DEBUG_LINK_TAG, "Failed to malloc for receive");
|
||||
@@ -286,7 +290,7 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
|
||||
}
|
||||
*recv_len = message_size;
|
||||
memcpy(data, message, message_size);
|
||||
// ESP_LOGI(DEBUG_LINK_TAG, "Received frame of type 0x%X destined for board %d", GET_TYPE(header.type_flag), header.receiver_id);
|
||||
ESP_LOGI(DEBUG_LINK_TAG, "Received frame of type 0x%X destined for board %d", GET_TYPE(header.type_flag), header.receiver_id);
|
||||
|
||||
//check for a rip frame
|
||||
if (static_cast<FrameType>(GET_TYPE(header.type_flag)) == FrameType::RIP_TABLE_CONTROL){
|
||||
@@ -295,7 +299,7 @@ esp_err_t DataLinkManager::receive(uint8_t* data, size_t data_len, size_t* recv_
|
||||
for (size_t i = 0; i < message_size-1; i+=2){
|
||||
uint8_t board_id = message[i];
|
||||
uint8_t hops = message[i+1];
|
||||
// ESP_LOGI(DEBUG_LINK_TAG, "Received: board_id %d and number of hops %d on channel %d", board_id, hops, curr_channel);
|
||||
ESP_LOGI(DEBUG_LINK_TAG, "Received: board_id %d and number of hops %d on channel %d", board_id, hops, curr_channel);
|
||||
|
||||
RIPRow* entry = nullptr;
|
||||
|
||||
@@ -689,7 +693,7 @@ esp_err_t DataLinkManager::send_rip_frame(bool broadcast, uint8_t dest_id){
|
||||
if (broadcast){
|
||||
res = send(BROADCAST_ADDR, rip_message, message_idx, FrameType::RIP_TABLE_CONTROL, 0);
|
||||
} else {
|
||||
// ESP_LOGI(DEBUG_LINK_TAG, "replying to discovery request to board %d", dest_id);
|
||||
ESP_LOGI(DEBUG_LINK_TAG, "replying to discovery request to board %d", dest_id);
|
||||
res = send(dest_id, rip_message, message_idx, FrameType::RIP_TABLE_CONTROL, FLAG_DISCOVERY);
|
||||
}
|
||||
if (res != ESP_OK){
|
||||
@@ -838,7 +842,7 @@ esp_err_t DataLinkManager::get_network_toplogy(RIPRow_public_matrix* matrix, siz
|
||||
while(true){
|
||||
bool dummy;
|
||||
xQueueReceive(link_layer_obj->manual_broadcasts, &dummy, pdMS_TO_TICKS(RIP_BROADCAST_INTERVAL)); //wait up to RIP_BROADCAST_INTERVAL ms
|
||||
// ESP_LOGI(DEBUG_LINK_TAG, "Broadcasting table..."); //debug
|
||||
ESP_LOGI(DEBUG_LINK_TAG, "Broadcasting table..."); //debug
|
||||
res = link_layer_obj->send_rip_frame(true, 0);
|
||||
if (res != ESP_OK){
|
||||
ESP_LOGE(DEBUG_LINK_TAG, "Failed to broadcast rip frame");
|
||||
|
||||
@@ -7,11 +7,11 @@
|
||||
#include "flatbuffers/flatbuffers.h"
|
||||
|
||||
// Ensure the included flatbuffers.h is the same version as when this file was
|
||||
// generated, otherwise it may not be compatible.
|
||||
//static_assert(FLATBUFFERS_VERSION_MAJOR == 25 &&
|
||||
// FLATBUFFERS_VERSION_MINOR == 2 &&
|
||||
// FLATBUFFERS_VERSION_REVISION == 10,
|
||||
// "Non-compatible flatbuffers version included");
|
||||
// // generated, otherwise it may not be compatible.
|
||||
// static_assert(FLATBUFFERS_VERSION_MAJOR == 25 &&
|
||||
// FLATBUFFERS_VERSION_MINOR == 2 &&
|
||||
// FLATBUFFERS_VERSION_REVISION == 10,
|
||||
// "Non-compatible flatbuffers version included");
|
||||
|
||||
struct MotorState;
|
||||
struct MotorStateBuilder;
|
||||
@@ -24,33 +24,36 @@ enum ModuleType : int8_t {
|
||||
ModuleType_SERVO_1 = 1,
|
||||
ModuleType_DC_MOTOR = 2,
|
||||
ModuleType_BATTERY = 3,
|
||||
ModuleType_SERVO_2 = 4,
|
||||
ModuleType_MIN = ModuleType_SPLITTER,
|
||||
ModuleType_MAX = ModuleType_BATTERY
|
||||
ModuleType_MAX = ModuleType_SERVO_2
|
||||
};
|
||||
|
||||
inline const ModuleType (&EnumValuesModuleType())[4] {
|
||||
inline const ModuleType (&EnumValuesModuleType())[5] {
|
||||
static const ModuleType values[] = {
|
||||
ModuleType_SPLITTER,
|
||||
ModuleType_SERVO_1,
|
||||
ModuleType_DC_MOTOR,
|
||||
ModuleType_BATTERY
|
||||
ModuleType_BATTERY,
|
||||
ModuleType_SERVO_2
|
||||
};
|
||||
return values;
|
||||
}
|
||||
|
||||
inline const char * const *EnumNamesModuleType() {
|
||||
static const char * const names[5] = {
|
||||
static const char * const names[6] = {
|
||||
"SPLITTER",
|
||||
"SERVO_1",
|
||||
"DC_MOTOR",
|
||||
"BATTERY",
|
||||
"SERVO_2",
|
||||
nullptr
|
||||
};
|
||||
return names;
|
||||
}
|
||||
|
||||
inline const char *EnumNameModuleType(ModuleType e) {
|
||||
if (::flatbuffers::IsOutRange(e, ModuleType_SPLITTER, ModuleType_BATTERY)) return "";
|
||||
if (::flatbuffers::IsOutRange(e, ModuleType_SPLITTER, ModuleType_SERVO_2)) return "";
|
||||
const size_t index = static_cast<size_t>(e);
|
||||
return EnumNamesModuleType()[index];
|
||||
}
|
||||
|
||||
@@ -642,7 +642,7 @@ int RMTManager::convert_symbols_to_char(rmt_symbol_word_t* symbols, size_t num,
|
||||
}
|
||||
i++;
|
||||
}
|
||||
printf("output_index %d\n", output_index);
|
||||
// printf("output_index %d\n", output_index);
|
||||
return (int)output_index;
|
||||
}
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ class RMTManager{
|
||||
|
||||
//rx_receive_config
|
||||
rmt_receive_config_t receive_config = {
|
||||
.signal_range_min_ns = 100,
|
||||
.signal_range_min_ns = 200,
|
||||
.signal_range_max_ns = 200 * 1000,
|
||||
.flags = {
|
||||
.en_partial_rx = true
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
idf_component_register(SRCS "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp" "CommunicationRouter.cpp" "MessagingInterface.cpp"
|
||||
idf_component_register(SRCS "WifiManager.cpp" "mDNSDiscoveryService.cpp" "TCPServer.cpp" "CommunicationRouter.cpp" "MessagingInterface.cpp" "OrientationDetection.cpp"
|
||||
PRIV_REQUIRES driver esp_event nvs_flash esp_netif esp_wifi espressif__mdns constants config flatbuffers dataLink rmt
|
||||
REQUIRES ptrQueue
|
||||
INCLUDE_DIRS "include")
|
||||
|
||||
@@ -5,13 +5,11 @@
|
||||
#include "mDNSDiscoveryService.h"
|
||||
#include "MPIMessageBuilder.h"
|
||||
#include "WifiManager.h"
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "Tables.h"
|
||||
|
||||
#include "PtrQueue.h"
|
||||
#include "OrientationDetection.h"
|
||||
|
||||
CommunicationRouter::~CommunicationRouter() {
|
||||
vTaskDelete(m_router_thread);
|
||||
@@ -139,9 +137,10 @@ std::pair<std::vector<uint8_t>, std::vector<Orientation>> CommunicationRouter::g
|
||||
}
|
||||
}
|
||||
|
||||
// todo: get orientation (temporary)
|
||||
for (int i = 0; i < MAX_WIRED_CONNECTIONS; i++) {
|
||||
connected_module_orientations[i] = Orientation_Deg0;
|
||||
if (const auto id = connected_module_ids[0]; 0 == id) {
|
||||
connected_module_orientations[0] = Orientation_Deg0;
|
||||
} else {
|
||||
connected_module_orientations[0] = OrientationDetection::get_orientation(0);
|
||||
}
|
||||
|
||||
return { connected_module_ids, connected_module_orientations };
|
||||
|
||||
46
components/rpc/OrientationDetection.cpp
Normal file
46
components/rpc/OrientationDetection.cpp
Normal file
@@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-07-26.
|
||||
//
|
||||
|
||||
#include <constants/module.h>
|
||||
#include "OrientationDetection.h"
|
||||
|
||||
#include <ConfigManager.h>
|
||||
#include <iostream>
|
||||
#include <bits/ostream.tcc>
|
||||
|
||||
void OrientationDetection::init() {
|
||||
for (int i = 0; i < MODULE_TO_NUM_CHANNELS_MAP[ConfigManager::get_module_type()]; i++) {
|
||||
setup_gpio(static_cast<gpio_num_t>(CHANNEL_TO_0_DEG_MAP[i]));
|
||||
setup_gpio(static_cast<gpio_num_t>(CHANNEL_TO_90_DEG_MAP[i]));
|
||||
setup_gpio(static_cast<gpio_num_t>(CHANNEL_TO_180_DEG_MAP[i]));
|
||||
setup_gpio(static_cast<gpio_num_t>(CHANNEL_TO_270_DEG_MAP[i]));
|
||||
}
|
||||
}
|
||||
|
||||
Orientation OrientationDetection::get_orientation(const uint8_t channel) {
|
||||
if (gpio_get_level(static_cast<gpio_num_t>(CHANNEL_TO_90_DEG_MAP[channel]))) {
|
||||
std::cout << "90deg" << std::endl;
|
||||
return Orientation_Deg90;
|
||||
} else if (gpio_get_level(static_cast<gpio_num_t>(CHANNEL_TO_180_DEG_MAP[channel]))) {
|
||||
std::cout << "180deg" << std::endl;
|
||||
return Orientation_Deg180;
|
||||
} else if (gpio_get_level(static_cast<gpio_num_t>(CHANNEL_TO_270_DEG_MAP[channel]))) {
|
||||
std::cout << "270deg" << std::endl;
|
||||
return Orientation_Deg270;
|
||||
} else {
|
||||
std::cout << "No orientation detected" << std::endl;
|
||||
return Orientation_Deg0;
|
||||
}
|
||||
}
|
||||
|
||||
void OrientationDetection::setup_gpio(const gpio_num_t pin) {
|
||||
const gpio_config_t io_conf = {
|
||||
.pin_bit_mask = (1ULL << pin),
|
||||
.mode = GPIO_MODE_INPUT,
|
||||
.pull_up_en = GPIO_PULLUP_DISABLE,
|
||||
.pull_down_en = GPIO_PULLDOWN_ENABLE,
|
||||
.intr_type = GPIO_INTR_DISABLE
|
||||
};
|
||||
gpio_config(&io_conf);
|
||||
}
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <chrono>
|
||||
#include <OrientationDetection.h>
|
||||
#include <WifiManager.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
@@ -40,6 +41,7 @@ public:
|
||||
m_pc_connection(std::move(pc_connection)),
|
||||
m_module_id(ConfigManager::get_module_id()),
|
||||
m_last_leader_updated(std::chrono::system_clock::now()){
|
||||
OrientationDetection::init();
|
||||
update_leader();
|
||||
|
||||
xTaskCreate(router_thread, "communication_router", 4096, this, 3, &this->m_router_thread);
|
||||
|
||||
20
components/rpc/include/OrientationDetection.h
Normal file
20
components/rpc/include/OrientationDetection.h
Normal file
@@ -0,0 +1,20 @@
|
||||
//
|
||||
// Created by Johnathon Slightham on 2025-07-26.
|
||||
//
|
||||
|
||||
#ifndef ORIENTATIONDETECTION_H
|
||||
#define ORIENTATIONDETECTION_H
|
||||
#include <flatbuffers_generated/RobotModule_generated.h>
|
||||
#include "driver/gpio.h"
|
||||
|
||||
class OrientationDetection {
|
||||
public:
|
||||
static void init();
|
||||
static Orientation get_orientation(uint8_t channel);
|
||||
|
||||
private:
|
||||
static void setup_gpio(gpio_num_t pin);
|
||||
|
||||
};
|
||||
|
||||
#endif //ORIENTATIONDETECTION_H
|
||||
Reference in New Issue
Block a user