Skip to content

Commit 767b406

Browse files
author
EternityFOR
committed
1. Add balanceshift remote adjust through app
2. Add tcp remote uart debug esp32
1 parent b907fcd commit 767b406

File tree

19 files changed

+775
-422
lines changed

19 files changed

+775
-422
lines changed

Self_Balance_Car_ESP32S3_N8R8/components/Data_handle/Data_handle.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ TimerHandle_t switch_back_to_original_ble_timer = NULL;
1717
void handle_pid_data(cJSON *root) {
1818
cJSON *balancePID = cJSON_GetObjectItemCaseSensitive(root, "balancePID");
1919
cJSON *velocityPID = cJSON_GetObjectItemCaseSensitive(root, "velocityPID");
20+
cJSON *balanceshift = cJSON_GetObjectItemCaseSensitive(root, "balanceshift");
2021

2122
if (!balancePID || !velocityPID) {
2223
ESP_LOGE(GATTS_DATA_TAG, "PID JSON does not contain required fields");
@@ -33,6 +34,12 @@ void handle_pid_data(cJSON *root) {
3334
pid_data.velocityPID.Kp = cJSON_GetObjectItemCaseSensitive(velocityPID, "Kp")->valuedouble;
3435
pid_data.velocityPID.Ki = cJSON_GetObjectItemCaseSensitive(velocityPID, "Ki")->valuedouble;
3536
pid_data.velocityPID.Kd = cJSON_GetObjectItemCaseSensitive(velocityPID, "Kd")->valuedouble;
37+
// Add check for balanceshift
38+
if (balanceshift != NULL) {
39+
pid_data.balanceshift = balanceshift->valuedouble;
40+
} else {
41+
pid_data.balanceshift = 0.0; // Set a default value if balanceshift is not present
42+
}
3643

3744
if (xQueueSend(msg_queue, &pid_data, portMAX_DELAY) != pdPASS) {
3845
ESP_LOGE(GATTS_DATA_TAG, "Failed to send PID data to queue");

Self_Balance_Car_ESP32S3_N8R8/components/Data_handle/include/Data_handle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ typedef struct {
2727
char weatherData[256];
2828
PIDParams balancePID;
2929
PIDParams velocityPID;
30+
float balanceshift;
3031
} msg_struct;
3132

3233
void handle_car_control_signal(char *car_control_data_str);

Self_Balance_Car_ESP32S3_N8R8/main/main.c

Lines changed: 138 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//***Head_Files***
1+
//***Head_Files***//
22
#include <stdio.h>
33
#include <stdlib.h>
44
#include <string.h>
@@ -16,14 +16,16 @@
1616
#include "wifi.h"
1717
#include "main.h"
1818
#include <driver/i2s_std.h>
19-
//***Parameters***
19+
#include "lwip/sockets.h"
20+
21+
//***Parameters***//
2022
//Tags
2123
#define MAINTAG "main"
2224
#define I2STAG "I2S"
25+
#define TCPTAG "TCP"
2326
//UART pin assign
2427
#define TXD_PIN (GPIO_NUM_17)
2528
#define RXD_PIN (GPIO_NUM_18)
26-
2729
/* I2S port and GPIOs */
2830
static bool stop_music_flag = true;
2931
static i2s_chan_handle_t tx_handle = NULL;
@@ -40,18 +42,21 @@ static i2s_chan_handle_t rx_handle = NULL;
4042
#define I2S_DI_IO GPIO_NUM_NC
4143
extern const uint8_t music_pcm_start[] asm("_binary_car_2_raw_start");
4244
extern const uint8_t music_pcm_end[] asm("_binary_car_2_raw_end");
43-
static TaskHandle_t i2s_music_task_handle = NULL; // Task handle for the music playback
44-
static bool is_music_playing = false; // Flag to track if music is playing
45-
45+
static TaskHandle_t i2s_music_task_handle = NULL;
46+
static bool is_music_playing = false;
47+
//TCP UART DEBUG
48+
#define TCP_PORT 8080
49+
#define TCP_TAG "TCP_SERVER"
50+
static int tcp_client_socket = -1;
51+
static int tcp_server_socket = -1;
52+
static struct sockaddr_in server_addr, client_addr;
53+
static TaskHandle_t tcp_server_task_handle = NULL;
54+
#define LOG_BUFFER_SIZE 512
55+
static char log_buffer[LOG_BUFFER_SIZE];
4656
//Queue
4757
QueueHandle_t msg_queue = NULL;
4858

49-
//Data type
50-
51-
//***Functions***
52-
53-
// Initializzation
54-
//uart init
59+
//***Functions***//
5560
void init_uart() {
5661
const uart_config_t uart_config = {
5762
.baud_rate = 115200,
@@ -63,17 +68,99 @@ void init_uart() {
6368
uart_param_config(UART_NUM_1, &uart_config);
6469
uart_set_pin(UART_NUM_1, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
6570
uart_driver_install(UART_NUM_1, 4096*5, 0, 0, NULL, 0);
71+
72+
uart_driver_install(UART_NUM_0, 4096*5, 0, 0, NULL, 0);
73+
6674
}
75+
int custom_log_vprintf(const char *fmt, va_list args) {
76+
int len = vsnprintf(log_buffer, LOG_BUFFER_SIZE, fmt, args);
77+
if (len > 0 && tcp_client_socket > 0) {
78+
int sent = send(tcp_client_socket, log_buffer, len, 0);
79+
if (sent < 0) {
80+
ESP_LOGE("LOG_TCP", "Failed to send log via TCP: errno %d", errno);
81+
}
82+
}
83+
return vprintf(fmt, args);
84+
}
85+
void setup_log_redirection() {
86+
esp_log_set_vprintf(custom_log_vprintf);
87+
}
88+
void tcp_server_task(void *pvParameters) {
89+
struct sockaddr_in server_addr, client_addr;
90+
socklen_t client_addr_len = sizeof(client_addr);
91+
92+
// 创建服务器套接字
93+
tcp_server_socket = socket(AF_INET, SOCK_STREAM, 0);
94+
if (tcp_server_socket < 0) {
95+
ESP_LOGE(TCPTAG, "Unable to create socket: errno %d", errno);
96+
vTaskDelete(NULL);
97+
return;
98+
}
6799

68-
//queue init
69-
void queue_init(){
70-
msg_queue = xQueueCreate(50, sizeof(msg_struct));
71-
if (msg_queue == NULL) {
72-
ESP_LOGE(MAINTAG, "Failed to create queue");
100+
// 设置服务器地址
101+
server_addr.sin_family = AF_INET;
102+
server_addr.sin_addr.s_addr = htonl(INADDR_ANY);
103+
server_addr.sin_port = htons(TCP_PORT);
104+
105+
// 绑定套接字
106+
if (bind(tcp_server_socket, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
107+
ESP_LOGE(TCPTAG, "Socket unable to bind: errno %d", errno);
108+
close(tcp_server_socket);
109+
vTaskDelete(NULL);
110+
return;
111+
}
112+
113+
// 开始监听
114+
if (listen(tcp_server_socket, 1) < 0) {
115+
ESP_LOGE(TCPTAG, "Error occurred during listen: errno %d", errno);
116+
close(tcp_server_socket);
117+
vTaskDelete(NULL);
118+
return;
119+
}
120+
ESP_LOGI(TCPTAG, "Socket listening on port %d", TCP_PORT);
121+
122+
// 主循环
123+
while (1) {
124+
vTaskDelay(1000 / portTICK_PERIOD_MS);
125+
ESP_LOGI(TCPTAG, "Waiting for a new client...");
126+
tcp_client_socket = accept(tcp_server_socket, (struct sockaddr *)&client_addr, &client_addr_len);
127+
if (tcp_client_socket < 0) {
128+
ESP_LOGE(TCPTAG, "Unable to accept connection: errno %d", errno);
129+
vTaskDelay(100 / portTICK_PERIOD_MS);
130+
continue;
131+
}
132+
ESP_LOGI(TCPTAG, "Client connected");
133+
134+
// 客户端通信循环
135+
while (1) {
136+
vTaskDelay(1000 / portTICK_PERIOD_MS);
137+
char recv_buf[64]; // 临时接收缓冲区
138+
int len = recv(tcp_client_socket, recv_buf, sizeof(recv_buf) - 1, 0);
139+
if (len <= 0) {
140+
ESP_LOGE(TCPTAG, "Client disconnected or error: errno %d", errno);
141+
close(tcp_client_socket);
142+
tcp_client_socket = -1; // 重置客户端套接字
143+
break;
144+
}
145+
146+
recv_buf[len] = '\0'; // 确保接收到的数据是字符串
147+
ESP_LOGI(TCPTAG, "Received data: %s", recv_buf);
148+
149+
// 示例:将收到的数据原样返回
150+
if (send(tcp_client_socket, recv_buf, len, 0) < 0) {
151+
ESP_LOGE(TCPTAG, "Error occurred during sending: errno %d", errno);
152+
close(tcp_client_socket);
153+
tcp_client_socket = -1; // 重置客户端套接字
154+
break;
155+
}
156+
}
73157
}
158+
159+
// 关闭服务器套接字
160+
close(tcp_server_socket);
161+
vTaskDelete(NULL);
74162
}
75163

76-
//i2s music
77164
static esp_err_t i2s_driver_init(void) {
78165
i2s_chan_config_t chan_cfg =
79166
I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM, I2S_ROLE_MASTER);
@@ -102,8 +189,6 @@ static esp_err_t i2s_driver_init(void) {
102189
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_handle, &std_cfg));
103190
return ESP_OK;
104191
}
105-
106-
107192
static void i2s_music(void *args)
108193
{
109194
esp_err_t ret = ESP_OK;
@@ -112,7 +197,6 @@ static void i2s_music(void *args)
112197
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle));
113198
while (1) {
114199
if (stop_music_flag) {
115-
// 停止播放逻辑
116200
// ESP_LOGI(I2STAG, "[music] Music playback stopped.");
117201
vTaskDelay(100 / portTICK_PERIOD_MS);
118202
continue;
@@ -146,7 +230,6 @@ static void i2s_music(void *args)
146230

147231
vTaskDelete(NULL);
148232
}
149-
150233
void start_music_playback() {
151234
if (!is_music_playing) {
152235
ESP_LOGI(MAINTAG, "Starting engine sound playback.");
@@ -156,7 +239,6 @@ void start_music_playback() {
156239
ESP_ERROR_CHECK(i2s_channel_enable(tx_handle));
157240
}
158241
}
159-
160242
void stop_music_playback() {
161243
if (is_music_playing) {
162244
ESP_LOGI(MAINTAG, "Stopping engine sound playback.");
@@ -167,15 +249,19 @@ void stop_music_playback() {
167249
}
168250
}
169251

170-
171-
// Main function to handle data and trigger music playback
252+
void queue_init(){
253+
msg_queue = xQueueCreate(20, sizeof(msg_struct));
254+
if (msg_queue == NULL) {
255+
ESP_LOGE(MAINTAG, "Failed to create queue");
256+
}
257+
}
172258
void send_msg_to_stm32(void *pvParameters) {
173259

174260
msg_struct msg_data;
261+
175262
while (1) {
263+
char uart_data[8192];
176264
if (xQueueReceive(msg_queue, &msg_data, portMAX_DELAY)) {
177-
char uart_data[2048];
178-
179265
switch (msg_data.type) {
180266
case DataType_Control:
181267
snprintf(uart_data, sizeof(uart_data), "{\"Type\":\"Control\",\"L\":%d,\"R\":%d,\"A\":%d}\n",
@@ -197,13 +283,14 @@ void send_msg_to_stm32(void *pvParameters) {
197283
break;
198284

199285
case DataType_PID:
200-
snprintf(uart_data, sizeof(uart_data), "{\"Type\":\"PID\",\"Balance_Kp\":%f,\"Balance_Ki\":%f,\"Balance_Kd\":%f,\"Velocity_Kp\":%f,\"Velocity_Ki\":%f,\"Velocity_Kd\":%f}\n",
286+
snprintf(uart_data, sizeof(uart_data), "{\"Type\":\"PID\",\"Balance_Kp\":%f,\"Balance_Ki\":%f,\"Balance_Kd\":%f,\"Velocity_Kp\":%f,\"Velocity_Ki\":%f,\"Velocity_Kd\":%f,\"BalanceShift\":%f}\n",
201287
msg_data.balancePID.Kp,
202288
msg_data.balancePID.Ki,
203289
msg_data.balancePID.Kd,
204290
msg_data.velocityPID.Kp,
205291
msg_data.velocityPID.Ki,
206-
msg_data.velocityPID.Kd);
292+
msg_data.velocityPID.Kd,
293+
msg_data.balanceshift);
207294
break;
208295

209296
case DataType_Mode_Control:
@@ -218,12 +305,14 @@ void send_msg_to_stm32(void *pvParameters) {
218305
uart_write_bytes(UART_NUM_1, uart_data, strlen(uart_data));
219306
ESP_LOGI(MAINTAG, "Sent data to UART: %s", uart_data);
220307
}
308+
vTaskDelay(100 / portTICK_PERIOD_MS);
221309
}
222310
}
223311

224312
//Main function
225313
void app_main(void)
226314
{
315+
BaseType_t result;
227316
// Initialize NVS
228317
esp_err_t ret;
229318
ret = nvs_flash_init();
@@ -232,26 +321,37 @@ void app_main(void)
232321
ret = nvs_flash_init();
233322
}
234323
ESP_ERROR_CHECK( ret );
324+
setup_log_redirection();
235325

236326
// Initialize WIFI
237327
initialise_wifi();
238-
239328
// Start getting time and weather
240329
// weather_time_task_init();
241-
330+
242331
//Queue init
243332
queue_init();
244-
245333
// Intialize UART
246334
init_uart();
247335

248-
//Initialize Blutooth
249-
ble_init();
250-
251336
//Task init
252-
xTaskCreate(send_msg_to_stm32, "send_msg_to_stm32", 4096*20, NULL, 10, NULL);
253-
337+
if(xTaskCreate(send_msg_to_stm32, "send_msg_to_stm32", 8192, NULL, 10, NULL) != pdPASS)
338+
{
339+
ESP_LOGE(MAINTAG, "Failed to create send_msg_to_stm32 task");
340+
}
254341
//I2S init
255342
i2s_driver_init();
256-
xTaskCreate(i2s_music, "i2s_music", 4096 * 5, NULL, 5, &i2s_music_task_handle);
343+
if(xTaskCreate(i2s_music, "i2s_music", 8192, NULL, 5, &i2s_music_task_handle) != pdPASS)
344+
{
345+
ESP_LOGE(MAINTAG, "Failed to create i2s_music task");
346+
}
347+
348+
349+
//Initialize Blutooth
350+
ble_init();
351+
if(xTaskCreate(tcp_server_task, "tcp_server_task", 4096, NULL, 6, &tcp_server_task_handle) != pdPASS)
352+
{
353+
ESP_LOGE(MAINTAG, "Failed to create tcp_server_task task");
354+
}
355+
size_t free_heap = xPortGetFreeHeapSize();
356+
printf("Free heap size: %d bytes\n", free_heap);
257357
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import socket
2+
3+
ESP32_IP = "192.168.50.24"
4+
PORT = 8080
5+
6+
def connect_to_server(ip, port):
7+
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
8+
client.connect((ip, port))
9+
print(f"Connected to {ip}:{port}")
10+
return client
11+
12+
while True:
13+
try:
14+
client = connect_to_server(ESP32_IP, PORT)
15+
while True:
16+
data = client.recv(1024)
17+
if not data:
18+
print("\nConnection lost.")
19+
break
20+
print(data.decode('utf-8'), end="")
21+
except (ConnectionError, OSError):
22+
print("\nConnection error occurred.")
23+
except KeyboardInterrupt:
24+
print("\nConnection closed by user.")
25+
break
26+
finally:
27+
try:
28+
client.close()
29+
except NameError:
30+
pass
31+
32+
user_input = input("Do you want to reconnect? (Y/N): ").strip().upper()
33+
if user_input != "Y":
34+
print("Exiting program.")
35+
break

0 commit comments

Comments
 (0)