Skip to content

Commit a13a511

Browse files
Implemented DiffDrive service
1 parent 6cba508 commit a13a511

File tree

10 files changed

+321
-98
lines changed

10 files changed

+321
-98
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE
5353
src/services/imu_service/imu_service.cpp
5454
src/services/power_service/power_service.cpp
5555
src/services/emergency_service/emergency_service.cpp
56+
src/services/diff_drive_service/diff_drive_service.cpp
5657
# BQ2567 driver
5758
src/drivers/bq_2576/bq_2576.cpp
5859
# VESC driver
@@ -83,6 +84,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME} PUBLIC
8384
target_add_service(${CMAKE_PROJECT_NAME} ImuService ${CMAKE_CURRENT_SOURCE_DIR}/services/imu_service.json)
8485
target_add_service(${CMAKE_PROJECT_NAME} PowerService ${CMAKE_CURRENT_SOURCE_DIR}/services/power_service.json)
8586
target_add_service(${CMAKE_PROJECT_NAME} EmergencyService ${CMAKE_CURRENT_SOURCE_DIR}/services/emergency_service.json)
87+
target_add_service(${CMAKE_PROJECT_NAME} DiffDriveService ${CMAKE_CURRENT_SOURCE_DIR}/services/diff_drive_service.json)
8688

8789
set_target_properties(${CMAKE_PROJECT_NAME}
8890
PROPERTIES SUFFIX ".elf")

boards/XCORE/mcuconf.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -359,16 +359,16 @@
359359
/*
360360
* SERIAL driver system settings.
361361
*/
362-
#define STM32_SERIAL_USE_USART1 FALSE
363-
#define STM32_SERIAL_USE_USART2 FALSE
362+
#define STM32_SERIAL_USE_USART1 TRUE
363+
#define STM32_SERIAL_USE_USART2 TRUE
364364
#define STM32_SERIAL_USE_USART3 FALSE
365-
#define STM32_SERIAL_USE_UART4 FALSE
365+
#define STM32_SERIAL_USE_UART4 TRUE
366366
#define STM32_SERIAL_USE_UART5 FALSE
367-
#define STM32_SERIAL_USE_USART6 FALSE
368-
#define STM32_SERIAL_USE_UART7 FALSE
369-
#define STM32_SERIAL_USE_UART8 FALSE
367+
#define STM32_SERIAL_USE_USART6 TRUE
368+
#define STM32_SERIAL_USE_UART7 TRUE
369+
#define STM32_SERIAL_USE_UART8 TRUE
370370
#define STM32_SERIAL_USE_UART9 FALSE
371-
#define STM32_SERIAL_USE_USART10 FALSE
371+
#define STM32_SERIAL_USE_USART10 TRUE
372372
#define STM32_SERIAL_USE_LPUART1 FALSE
373373

374374
/*
@@ -435,16 +435,16 @@
435435
/*
436436
* UART driver system settings.
437437
*/
438-
#define STM32_UART_USE_USART1 TRUE
439-
#define STM32_UART_USE_USART2 TRUE
438+
#define STM32_UART_USE_USART1 FALSE
439+
#define STM32_UART_USE_USART2 FALSE
440440
#define STM32_UART_USE_USART3 FALSE
441-
#define STM32_UART_USE_UART4 TRUE
441+
#define STM32_UART_USE_UART4 FALSE
442442
#define STM32_UART_USE_UART5 FALSE
443-
#define STM32_UART_USE_USART6 TRUE
444-
#define STM32_UART_USE_UART7 TRUE
445-
#define STM32_UART_USE_UART8 TRUE
443+
#define STM32_UART_USE_USART6 FALSE
444+
#define STM32_UART_USE_UART7 FALSE
445+
#define STM32_UART_USE_UART8 FALSE
446446
#define STM32_UART_USE_UART9 FALSE
447-
#define STM32_UART_USE_USART10 TRUE
447+
#define STM32_UART_USE_USART10 FALSE
448448
#define STM32_UART_USART1_RX_DMA_STREAM STM32_DMA_STREAM_ID_ANY
449449
#define STM32_UART_USART1_TX_DMA_STREAM STM32_DMA_STREAM_ID_ANY
450450
#define STM32_UART_USART2_RX_DMA_STREAM STM32_DMA_STREAM_ID_ANY

cfg/halconf.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
* @brief Enables the SERIAL subsystem.
143143
*/
144144
#if !defined(HAL_USE_SERIAL) || defined(__DOXYGEN__)
145-
#define HAL_USE_SERIAL FALSE
145+
#define HAL_USE_SERIAL TRUE
146146
#endif
147147

148148
/**
@@ -177,7 +177,7 @@
177177
* @brief Enables the UART subsystem.
178178
*/
179179
#if !defined(HAL_USE_UART) || defined(__DOXYGEN__)
180-
#define HAL_USE_UART TRUE
180+
#define HAL_USE_UART FALSE
181181
#endif
182182

183183
/**
@@ -415,7 +415,7 @@
415415
* buffers.
416416
*/
417417
#if !defined(SERIAL_BUFFERS_SIZE) || defined(__DOXYGEN__)
418-
#define SERIAL_BUFFERS_SIZE 16
418+
#define SERIAL_BUFFERS_SIZE 128
419419
#endif
420420

421421
/*===========================================================================*/

main.cpp

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,12 @@
2121
#include "services/emergency_service/emergency_service.hpp"
2222
#include "services/imu_service/imu_service.hpp"
2323
#include "services/power_service/power_service.hpp"
24+
#include "services/diff_drive_service/diff_drive_service.hpp"
2425
EmergencyService emergency_service{1};
26+
DiffDriveService diff_drive{2};
2527
ImuService imu_service{4};
2628
PowerService power_service{5};
2729

28-
static THD_WORKING_AREA(waTestThread, 500);
29-
30-
static void test_thread(void *p) {
31-
static VescUart vesc_uart(&UARTD2);
32-
while (1) {
33-
#ifdef USE_SEGGER_SYSTEMVIEW
34-
SEGGER_SYSVIEW_Print("keepalive");
35-
vesc_uart.sendKeepalive();
36-
SEGGER_SYSVIEW_Print("request");
37-
vesc_uart.requestVescValues();
38-
SEGGER_SYSVIEW_Print("parse");
39-
vesc_uart.parseVescValues();
40-
SEGGER_SYSVIEW_Print("wait");
41-
#endif
42-
chThdSleepMilliseconds(1000);
43-
}
44-
}
45-
4630
/*
4731
* Application entry point.
4832
*/
@@ -106,9 +90,7 @@ int main(void) {
10690
emergency_service.start();
10791
imu_service.start();
10892
power_service.start();
109-
110-
chThdCreateStatic(waTestThread, sizeof(waTestThread), NORMALPRIO, test_thread,
111-
NULL);
93+
diff_drive.start();
11294

11395
// Subscribe to global events and dispatch to our services
11496
event_listener_t event_listener;
@@ -125,9 +107,8 @@ int main(void) {
125107
uint32_t status_copy = mower_status;
126108
chMtxUnlock(&mower_status_mutex);
127109
// Notify services
110+
diff_drive.OnMowerStatusChanged(status_copy);
128111
}
129112
}
130113
}
131-
132-
chThdSleep(TIME_INFINITE);
133114
}

services/diff_drive_service.json

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,36 @@
1414
},
1515
{
1616
"id": 1,
17+
"name": "Left ESC Status",
18+
"type": "uint8_t"
19+
},
20+
{
21+
"id": 2,
1722
"name": "Left ESC Temperature",
1823
"type": "float"
1924
},
2025
{
21-
"id": 2,
26+
"id": 3,
2227
"name": "Left ESC Current",
2328
"type": "float"
2429
},
2530
{
26-
"id": 3,
31+
"id": 4,
32+
"name": "Right ESC Status",
33+
"type": "uint8_t"
34+
},
35+
{
36+
"id": 5,
2737
"name": "Right ESC Temperature",
2838
"type": "float"
2939
},
3040
{
31-
"id": 4,
41+
"id": 6,
3242
"name": "Right ESC Current",
3343
"type": "float"
3444
},
3545
{
36-
"id": 5,
46+
"id": 7,
3747
"name": "Wheel Ticks",
3848
"type": "uint32_t[2]"
3949
}

src/drivers/vesc/VescUart.cpp

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -17,57 +17,60 @@ int VescUart::receiveUartMessage(uint8_t* payloadReceived) {
1717
bool messageRead = false;
1818
uint8_t messageReceived[256];
1919
uint16_t lenPayload = 0;
20-
size_t bytes_read = 1;
21-
const auto start = chVTGetSystemTimeX();
22-
while (chVTTimeElapsedSinceX(start) < TIME_US2I(_TIMEOUT) &&
23-
messageRead == false) {
24-
while (uartReceiveTimeout(uart_, &bytes_read, messageReceived + counter,
25-
_TIMEOUT) == MSG_OK) {
26-
counter += bytes_read;
27-
// read one more byte
28-
bytes_read = 1;
29-
if (counter == 2) {
30-
switch (messageReceived[0]) {
31-
case 2:
32-
endMessage = messageReceived[1] +
33-
5; // Payload size + 2 for sice + 3 for SRC and End.
34-
lenPayload = messageReceived[1];
35-
36-
if (endMessage >= sizeof(messageReceived)) {
37-
// invalid message length, try again
38-
counter = 0;
39-
bytes_read = 1;
40-
} else {
41-
// we know exactly how many more bytes to read
42-
bytes_read = endMessage - counter;
43-
}
44-
break;
45-
46-
case 3:
47-
// ToDo: Add Message Handling > 255 (starting with 3)
48-
49-
break;
50-
51-
default:
52-
53-
break;
54-
}
20+
size_t bytes_read = 0;
21+
size_t bytes_to_read = 1;
22+
while (bytes_to_read > 0 &&
23+
(bytes_read = sdReadTimeout(uart_, messageReceived + counter,
24+
bytes_to_read, TIME_MS2I(_TIMEOUT))) > 0) {
25+
counter += bytes_read;
26+
// keep on reading, if a larger chunk was requested
27+
bytes_to_read -= bytes_read;
28+
// if only one byte was requested, read another one
29+
if (bytes_to_read == 0) {
30+
bytes_to_read = 1;
31+
}
32+
if (counter == 2) {
33+
switch (messageReceived[0]) {
34+
case 2:
35+
endMessage = messageReceived[1] +
36+
5; // Payload size + 2 for sice + 3 for SRC and End.
37+
lenPayload = messageReceived[1];
38+
39+
if (endMessage >= sizeof(messageReceived)) {
40+
// invalid message length, try again
41+
counter = 0;
42+
bytes_to_read = 1;
43+
} else {
44+
// we know exactly how many more bytes to read
45+
bytes_to_read = endMessage - counter;
46+
}
47+
break;
48+
49+
case 3:
50+
// ToDo: Add Message Handling > 255 (starting with 3)
51+
52+
break;
53+
54+
default:
55+
56+
break;
5557
}
58+
}
5659

57-
if (counter >= sizeof(messageReceived)) {
58-
counter = 0;
59-
break;
60-
}
60+
if (counter >= sizeof(messageReceived)) {
61+
counter = 0;
62+
break;
63+
}
6164

62-
if (counter == endMessage && messageReceived[endMessage - 1] == 3) {
63-
messageReceived[endMessage] = 0;
65+
if (counter == endMessage && messageReceived[endMessage - 1] == 3) {
66+
messageReceived[endMessage] = 0;
6467

65-
messageRead = true;
66-
break; // Exit if end of message is reached, even if there is still
67-
// more data in the buffer.
68-
}
68+
messageRead = true;
69+
break; // Exit if end of message is reached, even if there is still
70+
// more data in the buffer.
6971
}
7072
}
73+
7174
// if(messageRead == false && debugPort != nullptr ) {
7275
// debugPort->println("Timeout");
7376
// }
@@ -214,17 +217,17 @@ bool VescUart::processReadPacket(uint8_t* message) {
214217
}
215218
}
216219

217-
VescUart::VescUart(UARTDriver* uart_handle, uint32_t timeout_ms)
220+
VescUart::VescUart(SerialDriver* uart_handle, uint32_t timeout_ms)
218221
: _TIMEOUT(timeout_ms), uart_(uart_handle) {
219222
nunchuck.valueX = 127;
220223
nunchuck.valueY = 127;
221224
nunchuck.lowerButton = false;
222225
nunchuck.upperButton = false;
226+
}
227+
bool VescUart::startDriver() {
223228
// acquire the UART and never let go
224-
uartAcquireBus(uart_);
225-
uart_config_.speed = 115200;
226-
uart_config_.cr2 = USART_CR2_LINEN | USART_CR2_STOP1_BITS;
227-
uartStart(uart_, &uart_config_);
229+
serial_config_.speed = 115200;
230+
return sdStart(uart_, &serial_config_) == MSG_OK;
228231
}
229232
bool VescUart::getFWversion(void) { return getFWversion(0); }
230233

@@ -401,5 +404,5 @@ void VescUart::sendKeepalive(uint8_t canId) {
401404
packSendPayload(payload, payloadSize);
402405
}
403406
void VescUart::sendRaw(uint8_t* data, size_t size) {
404-
uartSendFullTimeout(uart_, &size, data, TIME_INFINITE);
407+
sdWrite(uart_, data, size);
405408
}

src/drivers/vesc/VescUart.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ class VescUart {
4747
/**
4848
* @brief Class constructor
4949
*/
50-
explicit VescUart(UARTDriver* uart, uint32_t timeout_ms = 100);
50+
explicit VescUart(SerialDriver* uart, uint32_t timeout_ms = 100);
51+
52+
bool startDriver();
5153

5254
/** Variabel to hold measurements returned from VESC */
5355
dataPackage data;
@@ -185,8 +187,8 @@ class VescUart {
185187
void printVescValues(void);
186188

187189
private:
188-
UARTDriver* uart_;
189-
UARTConfig uart_config_{};
190+
SerialDriver* uart_;
191+
SerialConfig serial_config_{};
190192
void sendRaw(uint8_t* data, size_t size);
191193

192194
/**

src/drivers/vesc/esc_status.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//
2+
// Created by clemens on 05.11.24.
3+
//
4+
5+
#ifndef ESC_STATUS_H
6+
#define ESC_STATUS_H
7+
enum {
8+
ESC_STATUS_DISCONNECTED = 99u,
9+
ESC_STATUS_ERROR = 100u,
10+
ESC_STATUS_STALLED = 150u,
11+
ESC_STATUS_OK = 200u,
12+
ESC_STATUS_RUNNING = 201u,
13+
};
14+
#endif // ESC_STATUS_H

0 commit comments

Comments
 (0)