@ -74,7 +74,8 @@ typedef struct{
typedef struct {
uint8_t checkout_1 ;
uint8_t checkout_2 ;
uint8_t index ;
uint8_t foot ; //1: 左脚, 2: 右脚
uint8_t package_index ;
sensor_package_t sensor_package [ MPU_FIFO_LEN ] ; //一次蓝牙发送MPU_FIFO_LEN组传感器数据
} ble_send_data_t ; //一次蓝牙发送的数据内容
@ -82,8 +83,12 @@ typedef struct{
static circle_buffer_t g_ble_send_cb ; // 环形缓冲区管理结构体
static ble_send_data_t g_sensor_data_storage [ SENSOR_DATA_BUFFER_SIZE ] ; //缓冲区
extern u8 foot_init ;
static OS_SEM receiver_ready_sem ; // 用于启动同步的信号量
static const uart_bus_t * uart_bus = NULL ;
static u16 test_id = 0 ;
//END -- 变量定义
//////////////////////////////////////////////////////////////////////////////////////////////////
@ -117,8 +122,9 @@ void sensor_collect_task(void){
signed short gyry_buf [ 100 ] ;
signed short gyrz_buf [ 100 ] ;
int fifo_num = 0 ;
static int data_index = 0 ;
u8 index = 1 ;
static int SL_ data_index = 0 ;
u8 package_ index = 1 ;
int tmp_index = 0 ;
while ( 1 ) { //4组地磁数据、16组六轴数据、1组气压计数据
interval + + ;
@ -130,84 +136,94 @@ void sensor_collect_task(void){
}
// xlog("MAG x: %.2f,y: %.2f,z: %.2f\n",mmc5603nj_buffer.x,mmc5603nj_buffer.y,mmc5603nj_buffer.z);
fifo_num = SL_SC7U22_FIFO_Read ( accx_buf , accy_buf , accz_buf , gyrx_buf , gyry_buf , gyrz_buf ) ; //40ms 一次性读取内置fifo的数据
for ( int i = 0 ; i < fifo_num ; i + + ) {
SL_SC7U22_FIFO_Read ( accx_buf , accy_buf , accz_buf , gyrx_buf , gyry_buf , gyrz_buf ) ; //一次性读取内置fifo的数据
for ( int i = 0 ; i < MPU_FIFO_LEN / 4 ; i + + ) {
tmp_index = SL_data_index + i ;
// if(tmp_index >= MPU_FIFO_LEN-1) tmp_index = MPU_FIFO_LEN-1;
send_data . sensor_package [ tmp_index ] . SC7U22_data [ 0 ] = accx_buf [ i ] ; //acc_x
send_data . sensor_package [ tmp_index ] . SC7U22_data [ 1 ] = accy_buf [ i ] ; //acc_y
send_data . sensor_package [ tmp_index ] . SC7U22_data [ 2 ] = accz_buf [ i ] ; //acc_z
send_data . sensor_package [ tmp_index ] . SC7U22_data [ 3 ] = gyrx_buf [ i ] ; //gyr_x
send_data . sensor_package [ tmp_index ] . SC7U22_data [ 4 ] = gyry_buf [ i ] ; //gyr_y
send_data . sensor_package [ tmp_index ] . SC7U22_data [ 5 ] = gyrz_buf [ i ] ; //gyr_z
// xlog(" Acc_x : %4d, Acc_y : %4d, Acc_z : %4d,\r\n", send_data.sensor_package[tmp_index].SC7U22_data[0], send_data.sensor_package[tmp_index].SC7U22_data[1], send_data.sensor_package[tmp_index].SC7U22_data[2]);
#if 0
float acc_g[3];
float gyr_dps[3];
acc_g[0] = (float)send_data.sensor_package[tmp_index].SC7U22_data[0] / 2048.0f;
acc_g[1] = (float)send_data.sensor_package[tmp_index].SC7U22_data[1] / 2048.0f;
acc_g[2] = (float)send_data.sensor_package[tmp_index].SC7U22_data[2] / 2048.0f;
gyr_dps[0] = (float)send_data.sensor_package[tmp_index].SC7U22_data[3] * 0.061f;
gyr_dps[1] = (float)send_data.sensor_package[tmp_index].SC7U22_data[4] * 0.061f;
gyr_dps[2] = (float)send_data.sensor_package[tmp_index].SC7U22_data[5] * 0.061f;
printf(" ACC(g): x=%.3f, y=%.3f, z=%.3f\n", acc_g[0], acc_g[1], acc_g[2]);
printf(" GYR(dps):x=%.3f, y=%.3f, z=%.3f\n", gyr_dps[0], gyr_dps[1], gyr_dps[2]);
#endif
}
SL_data_index + = MPU_FIFO_LEN / 4 ;
if ( interval > = 4 ) {
interval = 0 ;
SL_data_index = 0 ;
bmp280_read_data ( & temperature , & pressure ) ; //每40ms读取一次
for ( int i = 0 ; i < MPU_FIFO_LEN ; i + + ) {
send_data . sensor_package [ i ] . temperature = ( int16_t ) ( temperature * 1000.0f ) ;
send_data . sensor_package [ i ] . pressure = ( int32_t ) ( pressure * 1000.0f ) ;
}
// xlog("temperature: %.2f,pressure: %.2f\n",temperature,pressure);
// xlog("fifo_num:%d\n",fifo_num);
send_data . checkout_1 = 0xBE ;
send_data . checkout_2 = 0xBB ;
send_data . foot = foot_init ;
send_data . package_index = package_index ;
circle_buffer_write ( & g_ble_send_cb , & send_data ) ;
os_sem_post ( & receiver_ready_sem ) ; //通知另一个发送任务
memset ( & send_data , 0 , sizeof ( ble_send_data_t ) ) ;
memset ( & accx_buf , 0 , sizeof ( accx_buf ) ) ;
memset ( & accy_buf , 0 , sizeof ( accy_buf ) ) ;
memset ( & accz_buf , 0 , sizeof ( accz_buf ) ) ;
memset ( & gyrx_buf , 0 , sizeof ( gyrx_buf ) ) ;
memset ( & gyry_buf , 0 , sizeof ( gyry_buf ) ) ;
memset ( & gyrz_buf , 0 , sizeof ( gyrz_buf ) ) ;
fifo_num = SL_SC7U22_FIFO_Read ( accx_buf , accy_buf , accz_buf , gyrx_buf , gyry_buf , gyrz_buf ) ; //40ms一次性读取内置fifo的数据
bmp280_read_data ( & temperature , & pressure ) ; //每40ms读取一次
for ( int i = 0 ; i < MPU_FIFO_LEN ; i + + ) {
send_data . sensor_package [ i ] . SC7U22_data [ 0 ] = accx_buf [ i ] ; //acc_x
send_data . sensor_package [ i ] . SC7U22_data [ 1 ] = accy_buf [ i ] ; //acc_y
send_data . sensor_package [ i ] . SC7U22_data [ 2 ] = accz_buf [ i ] ; //acc_z
send_data . sensor_package [ i ] . SC7U22_data [ 3 ] = gyrx_buf [ i ] ; //gyr_x
send_data . sensor_package [ i ] . SC7U22_data [ 4 ] = gyry_buf [ i ] ; //gyr_y
send_data . sensor_package [ i ] . SC7U22_data [ 5 ] = gyrz_buf [ i ] ; //gyr_z
send_data . sensor_package [ i ] . temperature = ( int16_t ) ( temperature * 1000.0f ) ;
send_data . sensor_package [ i ] . pressure = ( int32_t ) ( pressure * 1000.0f ) ;
#if 0
float acc_g[3];
float gyr_dps[3];
acc_g[0] = (float)send_data.sensor_package[i].SC7U22_data[0] / 2048.0f;
acc_g[1] = (float)send_data.sensor_package[i].SC7U22_data[1] / 2048.0f;
acc_g[2] = (float)send_data.sensor_package[i].SC7U22_data[2] / 2048.0f;
gyr_dps[0] = (float)send_data.sensor_package[i].SC7U22_data[3] * 0.061f;
gyr_dps[1] = (float)send_data.sensor_package[i].SC7U22_data[4] * 0.061f;
gyr_dps[2] = (float)send_data.sensor_package[i].SC7U22_data[5] * 0.061f;
printf(" ACC(g): x=%.3f, y=%.3f, z=%.3f\n", acc_g[0], acc_g[1], acc_g[2]);
printf(" GYR(dps):x=%.3f, y=%.3f, z=%.3f\n", gyr_dps[0], gyr_dps[1], gyr_dps[2]);
#endif
}
// xlog("temperature: %.2f,pressure: %.2f\n",temperature,pressure);
// xlog("fifo_num:%d\n",fifo_num);
send_data . checkout_1 = 0xBE ;
send_data . checkout_2 = 0xBB ;
send_data . index = index ;
// circle_buffer_write(&g_ble_send_cb, &send_data);
// os_sem_post(&receiver_ready_sem); //通知另一个发送任务
memset ( & send_data , 0 , sizeof ( ble_send_data_t ) ) ;
index + + ;
if ( index > = 0xFF ) index = 1 ;
// xlog("=====================%d============================\n",get_ms_timer());
package_index + + ;
if ( package_index > = 0xFF ) package_index = 1 ;
// xlog("=====================%d============================\n",get_ms_timer());
}
os_time_dly ( 1 ) ; //10ms为单位
}
}
static int random = 0 ;
void data_log ( uint8_t * data ) {
static u8 imu_airplane [ MPU_FIFO_LEN ] [ 12 ] ;
// 检查数据包头部
if ( data [ 0 ] ! = 0xBE | | data [ 1 ] ! = 0xBB ) {
printf ( " Error: Invalid data packet header. \n " ) ;
return ;
}
//左右脚
uint8_t package_foot = data [ 2 ] ;
// 解析包索引
uint8_t index = data [ 2 ] ;
printf ( " --- Parsing Data Packet Index: %d --- \n " , index ) ;
uint8_t package_ index = data [ 3 ] ;
printf ( " --- Parsing Data Packet Index: %d --- \n " , package_ index) ;
uint8_t * p = & data [ 3 ] ; // 指向数据负载的起始位置
uint8_t * p = & data [ 4 ] ; // 指向数据负载的起始位置
// 循环解析16组数据
for ( int i = 0 ; i < MPU_FIFO_LEN ; i + + ) {
// 1. 解析六轴传感器数据 (12 bytes)
int16_t imu_raw [ 6 ] ;
for ( int j = 0 ; j < 6 ; j + + ) {
imu_airplane [ i ] [ 2 * j ] = p [ 0 ] ;
imu_airplane [ i ] [ 2 * j + 1 ] = p [ 1 ] ;
// 小端模式: 低字节在前, 高字节在后
imu_raw [ j ] = ( int16_t ) ( ( ( uint16_t ) p [ 1 ] < < 8 ) | ( uint16_t ) p [ 0 ] ) ;
p + = 2 ;
@ -244,16 +260,21 @@ void data_log(uint8_t* data){
float pressure = ( float ) press_raw / 1000.0f ;
// 打印解析后的数据
// if(i % 4 == 0){
printf( " Package[%d]:==================== \n " , i ) ;
// if(i % 8 == 0){
// printf(" ==================ble index: %d\n", *p);
// printf("Package[%d]:\n", i);
// printf(" ACC(g): x=%.3f, y=%.3f, z=%.3f\n", acc_g[0], acc_g[1], acc_g[2]);
// printf(" GYR(dps):x=%.3f, y=%.3f, z=%.3f\n", gyr_dps[0], gyr_dps[1], gyr_dps[2]);
printf( " MAG(Gs): x=%.3f, y=%.3f, z=%.3f\n " , mag_gauss [ 0 ] , mag_gauss [ 1 ] , mag_gauss [ 2 ] ) ;
printf( " TEMP(C): %.3f, PRESS(Pa): %.3f\n " , temperature, pressure) ;
// printf(" MAG(Gs): x=%.3f, y=%.3f, z=%.3f\n", mag_gauss[0], mag_gauss[1], mag_gauss[2]);
// printf(" TEMP(C): %.3f, PRESS(Pa): %.3f\n", temperature, pressure);
// }
}
printf( " --- End of Packet ---\n \n " ) ;
// printf(" --- End of Packet ---\n\n");
extern void uartSendData ( void * buf , u16 len ) ; // 确保u16是uint16_t或unsigned short
// uartSendData(imu_airplane, sizeof(imu_airplane));
uartSendData ( data , 484 ) ; // 发送总共17字节
}
/**
@ -262,7 +283,7 @@ void data_log(uint8_t* data){
*/
void BLE_send_fuc ( void ) {
ble_send_data_t send_data ;
uint8_t send_buffer [ 483 ] ;
uint8_t send_buffer [ 484 ] ;
while ( 1 ) {
os_sem_pend ( & receiver_ready_sem , 0 ) ; //阻塞等待
circle_buffer_read ( & g_ble_send_cb , & send_data ) ;
@ -271,7 +292,8 @@ void BLE_send_fuc(void){
uint8_t * p = send_buffer ;
* p + + = send_data . checkout_1 ;
* p + + = send_data . checkout_2 ;
* p + + = send_data . index ;
* p + + = send_data . foot ;
* p + + = send_data . package_index ;
for ( int i = 0 ; i < MPU_FIFO_LEN ; i + + ) {
sensor_package_t * pkg = & send_data . sensor_package [ i ] ;
@ -315,9 +337,11 @@ void BLE_send_fuc(void){
}
extern void uartSendData ( void * buf , u16 len ) ; // 确保u16是uint16_t或unsigned short
uartSendData ( send_buffer , 484 ) ; // 发送总共17字节
send_data_to_ble_client ( send_buffer , 484 ) ; // 发送数据
// send_data_to_ble_client(send_buffer, 3 + sizeof(send_data.sensor_package)); // 发送数据
data_log ( send_buffer ) ;
// data_log(send_buffer);
}
}
@ -342,8 +366,8 @@ void start_clloct(void){
*
*/
void stop_clloct ( void ) {
os_task_del_req ( " sensor_collect_task " ) ;
os_task_del_req ( " BLE_send_fuc " ) ;
os_task_del ( " sensor_collect_task " ) ;
os_task_del ( " BLE_send_fuc " ) ;
}
/**
@ -372,10 +396,40 @@ void xtell_task_create(void){
os_sem_create ( & receiver_ready_sem , 0 ) ;
extern void test_uart_init ( void ) ;
test_uart_init ( ) ;
}
/**
* @brief 发给上位机
*
*/
void test_uart_init ( void ) {
# if TCFG_UART0_ENABLE == 0
static u8 buff [ 40 ] ;
struct uart_platform_data_t u_arg = { 0 } ;
u_arg . tx_pin = IO_PORT_DP ;
u_arg . rx_cbuf = buff ;
u_arg . rx_cbuf_size = 32 ;
u_arg . frame_length = 6 ;
u_arg . rx_timeout = 100 ;
u_arg . isr_cbfun = NULL ;
u_arg . baud = 1000000 ;
u_arg . is_9bit = 0 ;
uart_bus = uart_dev_open ( & u_arg ) ;
# endif
}
void uartSendData ( void * buf , u16 len ) //发送数据的接口。
{
# if TCFG_UART0_ENABLE == 0
if ( uart_bus ) {
uart_bus - > write ( buf , len ) ; //把数据写到DMA
}
# endif
}
//////////////////////////////////////////////////////////////////////////////
//test
//
@ -385,50 +439,62 @@ static signed char acc_data_buf[BUFF_LEN] = {0};
// 1. 定义一个全局的信号量
static OS_SEM ble_send_sem ;
void on_ble_can_send ( void ) {
os_sem_post ( & ble_send_sem ) ;
}
int j = 0 ;
void data_send_task ( void ) {
while ( 1 ) {
// 等待“发送许可”信号量,如果没许可,任务会在此阻塞
// os_sem_pend(&ble_send_sem, 1);
// static signed char acc_data_buf[60] = {
// 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x22
// };
acc_data_buf [ 1 ] = ( uint8_t ) ( j & 0xFF ) ;
acc_data_buf [ 2 ] = ( uint8_t ) ( j > > 8 & 0xFF ) ;
acc_data_buf [ 3 ] = ( uint8_t ) ( j > > 16 & 0xFF ) ;
acc_data_buf [ 4 ] = ( uint8_t ) ( j > > 24 & 0xFF ) ;
signed short accx_buf [ 100 ] ;
signed short accy_buf [ 100 ] ;
signed short accz_buf [ 100 ] ;
signed short gyrx_buf [ 100 ] ;
signed short gyry_buf [ 100 ] ;
signed short gyrz_buf [ 100 ] ;
SL_SC7U22_FIFO_Read ( accx_buf , accy_buf , accz_buf , gyrx_buf , gyry_buf , gyrz_buf ) ; //一次性读取内置fifo的数据
acc_data_buf [ 0 ] = 0xAA ;
send_data_to_ble_client ( & acc_data_buf , 500 ) ;
// acc_data_buf[0] = 0xBB;
// send_data_to_ble_client(&acc_data_buf, 500);
// acc_data_buf[0] = 0xCC;
// send_data_to_ble_client(&acc_data_buf, 500);
// acc_data_buf[0] = 0xDD;
// send_data_to_ble_client(&acc_data_buf, 500);
# if 1
// 定义新的Packet ID和数据长度
# define PACKET_ID_RAW_IMU 0x04
# define PACKET_LENGTH_RAW_IMU 12 // 6个传感器值, 每个2字节
// static signed char acc_data_buf[20] = {
// 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
// };
// send_data_to_ble_client(&acc_data_buf, 20);
// send_data_to_ble_client(&acc_data_buf, 20);
// send_data_to_ble_client(&acc_data_buf, 20);
// xlog("===========time:%d\n",get_ms_timer());
// 声明一个发送缓冲区, 用于包含帧头、ID、长度、数据和校验和
// 帧头 (2) + ID (1) + 长度 (1) + 数据 (12) + 校验和 (1) = 17 字节
uint8_t tx_buffer [ 2 + 1 + 1 + PACKET_LENGTH_RAW_IMU + 1 ] ;
uint8_t checksum = 0 ;
int i ; // 用于循环计算校验和
j + + ;
os_time_dly ( 4 ) ; //10ms单位
// 填充帧头
tx_buffer [ 0 ] = 0xAA ;
tx_buffer [ 1 ] = 0xFF ;
// 填充Packet ID和长度
tx_buffer [ 2 ] = PACKET_ID_RAW_IMU ;
tx_buffer [ 3 ] = PACKET_LENGTH_RAW_IMU ;
// 填充原始传感器数据 (与你原先的processing_data内容相同)
tx_buffer [ 4 ] = ( uint8_t ) ( accx_buf [ 0 ] & 0xFF ) ; // accX LSB
tx_buffer [ 5 ] = ( uint8_t ) ( accx_buf [ 0 ] > > 8 & 0xFF ) ; // accX MSB
tx_buffer [ 6 ] = ( uint8_t ) ( accy_buf [ 0 ] & 0xFF ) ; // accY LSB
tx_buffer [ 7 ] = ( uint8_t ) ( accy_buf [ 0 ] > > 8 & 0xFF ) ; // accY MSB
tx_buffer [ 8 ] = ( uint8_t ) ( accz_buf [ 0 ] & 0xFF ) ; // accZ LSB
tx_buffer [ 9 ] = ( uint8_t ) ( accz_buf [ 0 ] > > 8 & 0xFF ) ; // accZ MSB
tx_buffer [ 10 ] = ( uint8_t ) ( gyrx_buf [ 0 ] & 0xFF ) ; // gyrX LSB
tx_buffer [ 11 ] = ( uint8_t ) ( gyrx_buf [ 0 ] > > 8 & 0xFF ) ; // gyrX MSB
tx_buffer [ 12 ] = ( uint8_t ) ( gyry_buf [ 0 ] & 0xFF ) ; // gyrY LSB
tx_buffer [ 13 ] = ( uint8_t ) ( gyry_buf [ 0 ] > > 8 & 0xFF ) ; // gyrY MSB
tx_buffer [ 14 ] = ( uint8_t ) ( gyrz_buf [ 0 ] & 0xFF ) ; // gyrZ LSB
tx_buffer [ 15 ] = ( uint8_t ) ( gyrz_buf [ 0 ] > > 8 & 0xFF ) ; // gyrZ MSB
// 计算校验和 (从 Packet ID 到所有数据字节的和)
checksum = tx_buffer [ 2 ] + tx_buffer [ 3 ] ; // ID + Length
for ( i = 0 ; i < PACKET_LENGTH_RAW_IMU ; i + + ) {
checksum + = tx_buffer [ 4 + i ] ; // 加上所有数据字节
}
tx_buffer [ 4 + PACKET_LENGTH_RAW_IMU ] = checksum ; // 校验和是最后一个字节
// 发送整个缓冲区
extern void uartSendData ( void * buf , u16 len ) ; // 确保u16是uint16_t或unsigned short
uartSendData ( tx_buffer , sizeof ( tx_buffer ) ) ; // 发送总共17字节
# endif
}
static u16 gtest_id = 0 ;
void test_func ( void ) {
@ -448,6 +514,8 @@ void test_func(void){
bmp280_init ( ) ;
os_task_create ( BLE_send_fuc , NULL , 5 , 1024 , 32 , " BLE_send_fuc " ) ;
os_task_create ( sensor_collect_task , NULL , 5 , 1024 , 32 , " sensor_collect_task " ) ;
// create_process(&test_id, "sensor_test",NULL,data_send_task ,3);
// data_send_task();
}
}