加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
runner.h 42.20 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
#ifndef RUNNER_H
#define RUNNER_H
#include <sensor_info.h>
#include <protocol.h>
#include <serial_port.h>
#include <QObject>
#include <QVariant>
#include <QDebug>
#include <QTimer>
#include <QQueue>
#include <QString>
#include <string.h>
#include "storage.h"
#include <QTime>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
typedef enum
{
BACKEND_EVENT_FC_PORT_NAME_LIST_UPDATE,
BACKEND_EVENT_FC_PORT_STATUS_UPDATE,
BACKEND_EVENT_DS_PORT_NAME_LIST_UPDATE,
BACKEND_EVENT_DS_PORT_STATUS_UPDATE,
BACKEND_EVENT_PRINT_INFO_UPDATE,
BACKEND_EVENT_FC_SERVO_EXPERIMENT_INFO_UPDATE,
BACKEND_EVENT_FC_SERVO_DYNAMIC_EXPERIMENT_INFO_UPDATE,
BACKEND_EVENT_FC_LIFT_EXPERIMENT_INFO_UPDATE,
BACKEND_EVENT_SENSOR_INFO_UPDATE,
BACKEND_EVENT_INPUT_INFO_UPDATE,
BACKEND_EVENT_ROLL_FFT_UPDATE,
BACKEND_EVENT_PITCH_FFT_UPDATE,
BACKEND_EVENT_YAW_FFT_UPDATE,
BACKEND_EVENT_SYSTEM_INITIAL,
BACKEND_EVENT_CNT
}backend_event_t;
typedef enum
{
EXPERIMENT_STATUS_IDLE,
EXPERIMENT_STATUS_READY,
EXPERIMENT_STATUS_START,
EXPERIMENT_STATUS_PROCESS,
EXPERIMENT_STATUS_FINISH,
}experiment_status_t;
typedef enum
{
FLIGHT_CONTROLER_SERIAL_PORT = 1,
DIGITAL_SAMPLER_SERIAL_PORT = 2,
}serial_port_type_t;
#define MAX_BACKEND_GROUP_CNT 2
class runner_t:public QObject
{
Q_OBJECT
public:
explicit runner_t(QObject *parent = nullptr);
~runner_t(void);
void fc_port_recv_callback(void)
{
fc_port.serial_port_recv_callback();
}
void ds_port_recv_callback(void)
{
ds_port.serial_port_recv_callback();
}
void system_timer_callback(void);
void fft_timer_callback(void);
void recv_data_parse(void)
{
fc_parse.recv_data_parse();
if( fc_parse.update_flag )
{
fc_parse.update_flag = false;
if( fc_parse.recv_header.pack_type == PACK_TYPE_SENSOR_INFO ) //sensor info
{
sensor.sensor_info_update((void*)&(fc_parse.p_sensor.pack));
// qDebug()<<"sensor info pack recv";
if( flight_status_record_status )
{
fc_flight_status_info_t info;
info.accel[AXIS_X] = sensor.sensor_info.inertia_data.accel[AXIS_X];
info.accel[AXIS_Y] = sensor.sensor_info.inertia_data.accel[AXIS_Y];
info.accel[AXIS_Z] = sensor.sensor_info.inertia_data.accel[AXIS_Z];
//accel
info.gyro[AXIS_X] = sensor.sensor_info.inertia_data.gyro[AXIS_X];
info.gyro[AXIS_Y] = sensor.sensor_info.inertia_data.gyro[AXIS_Y];
info.gyro[AXIS_Z] = sensor.sensor_info.inertia_data.gyro[AXIS_Z];
//gyro
info.velocity[AXIS_X] = sensor.sensor_info.velocity_data.vel[AXIS_X];
info.velocity[AXIS_Y] = sensor.sensor_info.velocity_data.vel[AXIS_Y];
info.velocity[AXIS_Z] = sensor.sensor_info.velocity_data.vel[AXIS_Z];
//velocity
info.position[AXIS_X] = sensor.sensor_info.position_data.pos[AXIS_X];
info.position[AXIS_Y] = sensor.sensor_info.position_data.pos[AXIS_Y];
info.position[AXIS_Z] = sensor.sensor_info.position_data.pos[AXIS_Z];
//position
info.euro[AXIS_X] = sensor.sensor_info.euro_data.roll;
info.euro[AXIS_Y] = sensor.sensor_info.euro_data.pitch;
info.euro[AXIS_Z] = sensor.sensor_info.euro_data.yaw;
//euro angle
info.quaternion[0] = sensor.sensor_info.quaternion_data.q[0];
info.quaternion[1] = sensor.sensor_info.quaternion_data.q[1];
info.quaternion[2] = sensor.sensor_info.quaternion_data.q[2];
info.quaternion[3] = sensor.sensor_info.quaternion_data.q[3];
//quaternion
info.voltage = sensor.sensor_info.voltage;
//voltage
info.motor_speed = sensor.input_info.motor_speed;
//motor speed
info.servo_angle[0] = sensor.input_info.servo_angle[0];
info.servo_angle[1] = sensor.input_info.servo_angle[1];
info.servo_angle[2] = sensor.input_info.servo_angle[2];
info.servo_angle[3] = sensor.input_info.servo_angle[3];
//servo angle
info.exp_euro[0] = sensor.expect_info.roll;
info.exp_euro[1] = sensor.expect_info.pitch;
info.exp_euro[2] = sensor.expect_info.yaw;
//expect euro
info.exp_gyro[0] = sensor.expect_info.gyrox;
info.exp_gyro[1] = sensor.expect_info.gyroy;
info.exp_gyro[2] = sensor.expect_info.gyroz;
//expect euro
info.exp_pos[0] = sensor.expect_info.x_pos;
info.exp_pos[1] = sensor.expect_info.y_pos;
info.exp_pos[2] = sensor.expect_info.z_pos;
//expect positon
info.exp_vel[0] = sensor.expect_info.x_vel;
info.exp_vel[1] = sensor.expect_info.y_vel;
info.exp_vel[2] = sensor.expect_info.z_vel;
//expect velocity
info.exp_mspeed = sensor.expect_info.motor_speed;
//expect motor speed
fc_flight_status_info_queue.enqueue(info);
}
sensor.sensor_update_flag = true;
// backend_event_set(BACKEND_EVENT_SENSOR_INFO_UPDATE);
}
else if( fc_parse.recv_header.pack_type == PACK_TYPE_INPUT_INFO ) //input info
{
// qDebug()<<"input info pack recv";
sensor.input_info_update((void*)&(fc_parse.p_input.pack));
sensor.input_update_flag = true;
// backend_event_set(BACKEND_EVENT_INPUT_INFO_UPDATE);
}
else if( fc_parse.recv_header.pack_type == PACK_TYPE_EXPECT_INFO )//expect info
{
// qDebug()<<"expect info pack recv";
sensor.expect_info_update((void*)&(fc_parse.p_expect.pack));
sensor.expect_update_flag = true;
}
else if( fc_parse.recv_header.pack_type == PACK_TYPE_PRINT_LOG ) //print log
{
//do nothing
}
else if( fc_parse.recv_header.pack_type == PACK_TYPE_FC_EXPERIMENT_DATA ) //experiment servo force fc
{
if( fc_parse.recv_experiment_type != current_experiment )
{
qDebug()<<"no matching experiment type current:"<<current_experiment<<"recv"<<fc_parse.recv_experiment_type;
}
else
{
uint8_t progress = 0;
if( fc_parse.recv_experiment_type == EXPERIMENT_TYPE_SERVO_FORCE ) //servo force experiment
{
fc_servo_force_storage_info_t tmp;
tmp.servo_angle[0] = fc_parse.fc_servo_force_info.servo1_angle;
tmp.servo_angle[1] = fc_parse.fc_servo_force_info.servo2_angle;
tmp.servo_angle[2] = fc_parse.fc_servo_force_info.servo3_angle;
tmp.servo_angle[3] = fc_parse.fc_servo_force_info.servo4_angle;
tmp.voltage = fc_parse.fc_servo_force_info.voltage;
tmp.throttle = fc_parse.fc_servo_force_info.throttle;
tmp.motor_speed = fc_parse.fc_servo_force_info.motor_speed;
fc_servo_force_info_queue.enqueue(tmp);
progress = fc_parse.fc_servo_force_info.experiment_progress;
backend_event_set(BACKEND_EVENT_FC_SERVO_EXPERIMENT_INFO_UPDATE);
}
else if( fc_parse.recv_experiment_type == EXPERIMENT_TYPE_SERVO_FORCE_DYNAMIC ) //servo force dynamic experiment
{
fc_servoforce_dynamic_storage_info_t tmp;
tmp.servo_angle = fc_parse.fc_servoforce_dynamic_info.pack.servo_angle;
tmp.voltage = fc_parse.fc_servoforce_dynamic_info.pack.voltage;
tmp.throttle = fc_parse.fc_servoforce_dynamic_info.pack.throttle;
tmp.motor_speed = fc_parse.fc_servoforce_dynamic_info.pack.motor_speed;
fc_servoforce_dynamic_info_queue.enqueue(tmp);
progress = fc_parse.fc_servoforce_dynamic_info.pack.progress;
backend_event_set(BACKEND_EVENT_FC_SERVO_DYNAMIC_EXPERIMENT_INFO_UPDATE);
}
else if( fc_parse.recv_experiment_type == EXPERIMENT_TYPE_MOTOR_LIFT ) //lift experiment
{
fc_lift_storage_info_t tmp;
tmp.servo_angle = fc_parse.fc_lift_info.pack.servo_angle;
tmp.voltage = fc_parse.fc_lift_info.pack.voltage;
tmp.throttle = fc_parse.fc_lift_info.pack.throttle;
tmp.motor_speed = fc_parse.fc_lift_info.pack.motor_speed;
fc_lift_info_queue.enqueue(tmp);
progress = fc_parse.fc_lift_info.pack.progress;
backend_event_set(BACKEND_EVENT_FC_LIFT_EXPERIMENT_INFO_UPDATE);
}
else
{
qDebug()<<"invalid experiment type"<<fc_parse.recv_experiment_type;
}
if( progress == 100 )
{
//experiment finish
qDebug() << "experiment progress is 100,complete";
experiment_status = EXPERIMENT_STATUS_FINISH;
}
if(experiment_pause_flag == true)
{
//declare that pause failed
experiment_pause_flag = false;
}
}
}
else if( fc_parse.recv_header.pack_type == PACK_TYPE_ACK )
{
if( fc_parse.ack.ack_type == PACK_TYPE_FC_EXPERIMENT_CMD )
{
if( fc_parse.ack.ack_result == 1 )
{
if( experiment_status == EXPERIMENT_STATUS_IDLE )
{
qDebug() << "ready for experiment";
experiment_status = EXPERIMENT_STATUS_READY;
}
else if( experiment_status == EXPERIMENT_STATUS_READY )
{
qDebug() << "start experiment";
experiment_status = EXPERIMENT_STATUS_START;
}
}
else if( fc_parse.ack.ack_result == 0xff )
{
if( experiment_status == EXPERIMENT_STATUS_READY )
{
experiment_status = EXPERIMENT_STATUS_IDLE;
}
}
}
}
}
ds_parse.recv_data_parse();
if( ds_parse.update_flag == true )
{
ds_parse.update_flag = false;
if( ds_parse.recv_header.pack_type == PACK_TYPE_PRINT_LOG )
{
//do nothing
}
else if( ds_parse.recv_header.pack_type == PACK_TYPE_ACK )
{
}
else if( ds_parse.recv_header.pack_type == PACK_TYPE_DS_EXPERIMENT_DATA )
{
if( experiment_status == EXPERIMENT_STATUS_PROCESS && experiment_pause_flag == false )
{
//digital sampler data
ds_storage_info_t tmp;
tmp.voltage_ch[0] = ds_parse.digital_sampler_info.pack.voltage_ch1;
tmp.voltage_ch[1] = ds_parse.digital_sampler_info.pack.voltage_ch2;
tmp.voltage_ch[2] = ds_parse.digital_sampler_info.pack.voltage_ch3;
tmp.voltage_ch[3] = ds_parse.digital_sampler_info.pack.voltage_ch4;
ds_info_queue.enqueue(tmp);
}
}
}
}
int32_t backend_event_set(backend_event_t event);
int32_t backend_event_clear(backend_event_t event);
int32_t backend_event_execute(backend_event_t event);
bool backend_event_get_status(backend_event_t event);
Q_INVOKABLE QVariantMap get_port_info(serial_port_type_t type)
{
QVariantMap map;
map.clear();
if( type == FLIGHT_CONTROLER_SERIAL_PORT )
{
map.insert("port_cnt",fc_port.port_cnt);
map.insert("port_status",fc_port.port_status);
map.insert("port_name_list",fc_port.port_name_list);
map.insert("current_port_name",fc_port.current_port_name);
}
else if( type == DIGITAL_SAMPLER_SERIAL_PORT )
{
map.insert("port_cnt",ds_port.port_cnt);
map.insert("port_status",ds_port.port_status);
map.insert("port_name_list",ds_port.port_name_list);
map.insert("current_port_name",ds_port.current_port_name);
}
return map;
}
/* port search button callback*/
Q_INVOKABLE void search_port_button_callback(serial_port_type_t type)
{
if( type == FLIGHT_CONTROLER_SERIAL_PORT )
{
fc_port.search_serial_port();
backend_event_set(BACKEND_EVENT_FC_PORT_NAME_LIST_UPDATE);
}
else if( type == DIGITAL_SAMPLER_SERIAL_PORT )
{
ds_port.search_serial_port();
backend_event_set(BACKEND_EVENT_DS_PORT_NAME_LIST_UPDATE);
}
}
/* open&close port button callback*/
Q_INVOKABLE void open_port_button_callback(QString current_port,serial_port_type_t type) //开关串口按钮
{
if( type == FLIGHT_CONTROLER_SERIAL_PORT )
{
fc_port.current_port_name = current_port;
fc_port.open_serial_port();
backend_event_set(BACKEND_EVENT_FC_PORT_STATUS_UPDATE);
}
else if( type == DIGITAL_SAMPLER_SERIAL_PORT )
{
ds_port.current_port_name = current_port;
ds_port.open_serial_port();
backend_event_set(BACKEND_EVENT_DS_PORT_STATUS_UPDATE);
}
}
Q_INVOKABLE void record_switch_button_callback(void) //记录开关按钮
{
if( flight_status_record_status == false )
{
QString fold_name;
fold_name = "flight_status_" + QTime::currentTime().toString("hh:mm:ss");
fold_name.replace(':','_');
QDir dir;
if (!dir.exists(fold_name))
{
if (dir.mkdir(fold_name))
{
qDebug() << "文件夹创建成功!";
}
else
{
qDebug() << "文件夹创建失败!";
}
}
else
{
qDebug() << "文件夹已存在!";
}
gyro_file.setFileName( fold_name + '/' + file_name_gyro );
accel_file.setFileName( fold_name + '/' + file_name_accel );
vel_file.setFileName( fold_name + '/' + file_name_vel );
euro_file.setFileName( fold_name + '/' + file_name_euro );
pos_file.setFileName( fold_name + '/' + file_name_pos );
quaternion_file.setFileName( fold_name + '/' + file_name_quaternion );
voltage_file.setFileName( fold_name + '/' + file_name_voltage );
servo_file.setFileName( fold_name + '/' + file_name_servo );
ms_file.setFileName( fold_name + '/' + file_name_motorspeed );
exp_gyro_file.setFileName( fold_name + '/' + file_name_exp_gyro );
exp_euro_file.setFileName( fold_name + '/' + file_name_exp_euro );
exp_pos_file.setFileName( fold_name + '/' + file_name_exp_pos );
exp_vel_file.setFileName( fold_name + '/' + file_name_exp_vel );
exp_mspeed_file.setFileName( fold_name + '/' + file_name_exp_mspeed );
if( !gyro_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << gyro_file.fileName();
}
if( !accel_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << accel_file.fileName();
}
if( !vel_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << vel_file.fileName();
}
if( !euro_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << euro_file.fileName();
}
if( !pos_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << pos_file.fileName();
}
if( !quaternion_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << quaternion_file.fileName();
}
if( !voltage_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << voltage_file.fileName();
}
if( !servo_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << servo_file.fileName();
}
if( !ms_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << ms_file.fileName();
}
if( !exp_gyro_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << exp_gyro_file.fileName();
}
if( !exp_euro_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << exp_euro_file.fileName();
}
if( !exp_pos_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << exp_pos_file.fileName();
}
if( !exp_vel_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << exp_vel_file.fileName();
}
if( !exp_mspeed_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
qDebug()<<"open file failed -- " << exp_mspeed_file.fileName();
}
fc_flight_status_info_queue.clear();
flight_status_record_status = true;
//开始记录,创建文件路径,设置记录状态
}
else
{
gyro_file.close();
accel_file.close();
vel_file.close();
euro_file.close();
pos_file.close();
quaternion_file.close();
voltage_file.close();
servo_file.close();
ms_file.close();
exp_gyro_file.close();
exp_euro_file.close();
exp_pos_file.close();
exp_vel_file.close();
exp_mspeed_file.close();
flight_status_record_status = false;
//结束记录,保存并关闭文件,设置记录状态
}
}
Q_INVOKABLE QVariantMap get_record_status(void) //获取记录开关的状态
{
QVariantMap map;
map.clear();
map.insert("status",flight_status_record_status==true?1:0);
return map;
}
Q_INVOKABLE void experiment_button_callback(experiment_button_type_t btn,QString exp_type)
{
experiment_type_t type_value;
bool is_find = false;
for (auto it = experiment_list_text.begin(); it != experiment_list_text.end(); ++it)
{
if(it.key() == exp_type)
{
qDebug() << "find the experiment -- " << it.key() << ":" << it.value();
type_value = it.value();
is_find = true;
current_experiment = type_value;
break;
}
}
if( is_find == false )
{
qDebug() << "invalid experiment";
return;
}
QByteArray buf;
buf.clear();
if(btn == EXPERIMENT_BUTTON_START)
{
if( experiment_status == EXPERIMENT_STATUS_READY )
{
if( fc_port.port_status == true )
{
qDebug()<<"experiment start button type"<<"experiment type:"<<exp_type;
int len = 0;
fc_parse.experiment_command_pack(buf,type_value,EXPERIMENT_CMD_TYPE_START); //
len = fc_port.serial_port_send(buf);
if( len <= 0 )
{
qDebug()<<"send failed,check the port status,ret:"<<len;
}
}
else
{
qDebug() << "please open port";
}
}
else
{
qDebug()<<"experiment cant start,cuze current status is:"<<experiment_status;
}
}
else if(btn == EXPERIMENT_BUTTON_PAUSE)
{
if( experiment_status == EXPERIMENT_STATUS_PROCESS)
{
if( fc_port.port_status == true )
{
experiment_pause_flag = !experiment_pause_flag;
qDebug()<<"experiment pause button type"<<"experiment type:"<<exp_type;
int len = 0;
fc_parse.experiment_command_pack(buf,type_value,EXPERIMENT_CMD_TYPE_PAUSE);
len = fc_port.serial_port_send(buf);
if( len <= 0 )
{
qDebug()<<"send failed,check the port status,ret:"<<len;
}
}
else
{
qDebug() << "please open port";
}
}
else
{
qDebug()<<"experiment not start,cant pause";
}
}
else if(btn == EXPERIMENT_BUTTON_TERMINATE)
{
if( fc_port.port_status == true )
{
if( experiment_status == EXPERIMENT_STATUS_PROCESS )
{
experiment_status = EXPERIMENT_STATUS_FINISH;
}
qDebug()<<"experiment terminate button type"<<"experiment type:"<<exp_type;
int len = 0;
fc_parse.experiment_command_pack(buf,type_value,EXPERIMENT_CMD_TYPE_TERMINATE);
len = fc_port.serial_port_send(buf);
if( len <= 0 )
{
qDebug()<<"send failed,check the port status,ret:"<<len;
}
}
else
{
qDebug() << "please open port";
}
}
else
{
qDebug()<<"invalid button type";
}
buf.clear();
}
void flight_record_manager(void) //飞行记录管理
{
if( flight_status_record_status == true && fc_flight_status_info_queue.isEmpty() == false)
{
fc_flight_status_info_t storage_info;
storage_info = fc_flight_status_info_queue.dequeue();
QString write_text = "";
write_text = QString::number(storage_info.servo_angle[0],'f',3) + " " +
QString::number(storage_info.servo_angle[1],'f',3) + " " +
QString::number(storage_info.servo_angle[2],'f',3) + " " +
QString::number(storage_info.servo_angle[3],'f',3) + "\n";
QTextStream out1(&servo_file);
out1 << write_text;
//servo angle
write_text = QString::number(storage_info.gyro[0],'f',3) + " " +
QString::number(storage_info.gyro[1],'f',3) + " " +
QString::number(storage_info.gyro[2],'f',3) + "\n";
QTextStream out2(&gyro_file);
out2 << write_text;
//gyro
write_text = QString::number(storage_info.accel[0],'f',3) + " " +
QString::number(storage_info.accel[1],'f',3) + " " +
QString::number(storage_info.accel[2],'f',3) + "\n";
QTextStream out3(&accel_file);
out3 << write_text;
//accel
write_text = QString::number(storage_info.velocity[0],'f',3) + " " +
QString::number(storage_info.velocity[1],'f',3) + " " +
QString::number(storage_info.velocity[2],'f',3) + "\n";
QTextStream out4(&vel_file);
out4 << write_text;
//velocity
write_text = QString::number(storage_info.position[0],'f',3) + " " +
QString::number(storage_info.position[1],'f',3) + " " +
QString::number(storage_info.position[2],'f',3) + "\n";
QTextStream out5(&pos_file);
out5 << write_text;
//position
write_text = QString::number(storage_info.euro[0],'f',3) + " " +
QString::number(storage_info.euro[1],'f',3) + " " +
QString::number(storage_info.euro[2],'f',3) + "\n";
QTextStream out6(&euro_file);
out6 << write_text;
//euro angle
write_text = QString::number(storage_info.quaternion[0],'f',3) + " " +
QString::number(storage_info.quaternion[1],'f',3) + " " +
QString::number(storage_info.quaternion[2],'f',3) + " " +
QString::number(storage_info.quaternion[3],'f',3) + "\n";
QTextStream out7(&quaternion_file);
out7 << write_text;
//quaternion
write_text = QString::number(storage_info.voltage,'f',3) + "\n";
QTextStream out8(&voltage_file);
out8 << write_text;
//voltage
write_text = QString::number(storage_info.motor_speed,'f',3) + "\n";
QTextStream out9(&ms_file);
out9 << write_text;
//motor speed
write_text = QString::number(storage_info.exp_euro[0],'f',3) + " " +
QString::number(storage_info.exp_euro[1],'f',3) + " " +
QString::number(storage_info.exp_euro[2],'f',3) + "\n";
QTextStream out10(&exp_euro_file);
out10 << write_text;
//expect euro angle
write_text = QString::number(storage_info.exp_gyro[0],'f',3) + " " +
QString::number(storage_info.exp_gyro[1],'f',3) + " " +
QString::number(storage_info.exp_gyro[2],'f',3) + "\n";
QTextStream out11(&exp_gyro_file);
out11 << write_text;
//expect gyro
write_text = QString::number(storage_info.exp_pos[0],'f',3) + " " +
QString::number(storage_info.exp_pos[1],'f',3) + " " +
QString::number(storage_info.exp_pos[2],'f',3) + "\n";
QTextStream out12(&exp_pos_file);
out12 << write_text;
//expect position
write_text = QString::number(storage_info.exp_vel[0],'f',3) + " " +
QString::number(storage_info.exp_vel[1],'f',3) + " " +
QString::number(storage_info.exp_vel[2],'f',3) + "\n";
QTextStream out13(&exp_vel_file);
out13 << write_text;
//expect velocity
write_text = QString::number(storage_info.exp_mspeed,'f',3) + "\n";
QTextStream out14(&exp_mspeed_file);
out14 << write_text;
//expect motor speed
}
}
void experiment_manager(void) //实验管理
{
static uint32_t cnt = 0;
static experiment_status_t last_status = EXPERIMENT_STATUS_IDLE;
if( last_status != experiment_status )
{
//status changed
qDebug()<<"experiment status changed" << last_status << "->" << experiment_status;
emit experiment_status_change_signal();
last_status = experiment_status;
}
if( experiment_status == EXPERIMENT_STATUS_IDLE )
{
//idle status: query flight controler status
cnt++;
if( cnt % 1000 == 0) //avoid sending too much
{
if( fc_port.port_status == true && ds_port.port_status == true )
{
QByteArray buf;
buf.clear();
int len = 0;
fc_parse.experiment_command_pack(buf,EXPERIMENT_TYPE_SERVO_FORCE,EXPERIMENT_CMD_TYPE_QUERY);
len = fc_port.serial_port_send(buf);
if( len <= 0 )
{
qDebug()<<"send failed,check the port status,ret:"<<len;
}
qDebug()<<"Send query ";
}
else
{
// qDebug() << "please open port";
}
}
}
else if( experiment_status == EXPERIMENT_STATUS_START )
{
QString fold_name;
if( current_experiment == EXPERIMENT_TYPE_SERVO_FORCE )
{
fc_servo_force_info_queue.clear();
fold_name = "servo_force_" + QTime::currentTime().toString("hh:mm:ss");
}
else if( current_experiment == EXPERIMENT_TYPE_SERVO_FORCE_DYNAMIC )
{
fc_servoforce_dynamic_info_queue.clear();
fold_name = "servo_force_dynamic_" + QTime::currentTime().toString("hh:mm:ss");
}
else if( current_experiment == EXPERIMENT_TYPE_MOTOR_LIFT )
{
fc_lift_info_queue.clear();
fold_name = "lift_" + QTime::currentTime().toString("hh:mm:ss");
}
else
{
fold_name = "unKnown" + QTime::currentTime().toString("hh:mm:ss");
}
fold_name.replace(':','_');
QDir dir;
if (!dir.exists(fold_name))
{
if (dir.mkdir(fold_name))
{
qDebug() << "文件夹创建成功!";
}
else
{
qDebug() << "文件夹创建失败!";
}
}
else
{
qDebug() << "文件夹已存在!";
}
experiment_digital_sampler_file.setFileName( fold_name + '/' + file_name_digital_sampler );
experiment_voltage_file.setFileName( fold_name + '/' + file_name_voltage );
experiment_servo_angle_file.setFileName( fold_name + '/' + file_name_servo );
experiment_motor_speed_file.setFileName( fold_name + '/' + file_name_motorspeed );
experiment_throttle_file.setFileName( fold_name + '/' + file_name_throttle );
bool is_failed = false;
if( !experiment_digital_sampler_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
is_failed = true;
qDebug()<<"open file failed -- " << experiment_digital_sampler_file.fileName();
}
if( !experiment_voltage_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
is_failed = true;
qDebug()<<"open file failed -- " << experiment_voltage_file.fileName();
}
if( !experiment_servo_angle_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
is_failed = true;
qDebug()<<"open file failed -- " << experiment_servo_angle_file.fileName();
}
if( !experiment_motor_speed_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
is_failed = true;
qDebug()<<"open file failed -- " << experiment_motor_speed_file.fileName();
}
if( !experiment_throttle_file.open(QIODevice::WriteOnly | QIODevice::Text))
{
is_failed = true;
qDebug()<<"open file failed -- " << experiment_throttle_file.fileName();
}
if( is_failed )
{
experiment_status = EXPERIMENT_STATUS_IDLE;
QByteArray buf;
buf.clear();
int len = 0;
fc_parse.experiment_command_pack(buf,current_experiment,EXPERIMENT_CMD_TYPE_TERMINATE);
len = fc_port.serial_port_send(buf);
if( len <= 0 )
{
qDebug()<<"send failed,check the port status,ret:"<<len;
}
}
else
{
experiment_status = EXPERIMENT_STATUS_PROCESS;
}
}
else if( experiment_status == EXPERIMENT_STATUS_PROCESS )
{
QString write_text = "";
if( current_experiment == EXPERIMENT_TYPE_SERVO_FORCE )
{
if( fc_servo_force_info_queue.isEmpty() == false )
{
fc_servo_force_storage_info_t storage_info;
storage_info = fc_servo_force_info_queue.dequeue();
write_text = QString::number(storage_info.servo_angle[0],'f',3) + " " +
QString::number(storage_info.servo_angle[1],'f',3) + " " +
QString::number(storage_info.servo_angle[2],'f',3) + " " +
QString::number(storage_info.servo_angle[3],'f',3) + "\n";
QTextStream out1(&experiment_servo_angle_file);
out1 << write_text;
write_text = QString::number(storage_info.throttle,'d') + "\n";
QTextStream out2(&experiment_throttle_file);
out2 << write_text;
write_text = QString::number(storage_info.voltage,'f',3) + "\n";
QTextStream out3(&experiment_voltage_file);
out3 << write_text;
write_text = QString::number(storage_info.motor_speed,'f',3) + "\n";
QTextStream out4(&experiment_motor_speed_file);
out4 << write_text;
}
}
else if( current_experiment == EXPERIMENT_TYPE_SERVO_FORCE_DYNAMIC )
{
if( fc_servoforce_dynamic_info_queue.isEmpty() == false )
{
fc_servoforce_dynamic_storage_info_t storage_info;
storage_info = fc_servoforce_dynamic_info_queue.dequeue();
write_text = QString::number(storage_info.servo_angle,'f',3) + "\n";
QTextStream out1(&experiment_servo_angle_file);
out1 << write_text;
write_text = QString::number(storage_info.throttle,'d') + "\n";
QTextStream out2(&experiment_throttle_file);
out2 << write_text;
write_text = QString::number(storage_info.voltage,'f',3) + "\n";
QTextStream out3(&experiment_voltage_file);
out3 << write_text;
write_text = QString::number(storage_info.motor_speed,'f',3) + "\n";
QTextStream out4(&experiment_motor_speed_file);
out4 << write_text;
}
}
else if( current_experiment == EXPERIMENT_TYPE_MOTOR_LIFT )
{
if( fc_lift_info_queue.isEmpty() == false )
{
fc_lift_storage_info_t storage_info;
storage_info = fc_lift_info_queue.dequeue();
write_text = QString::number(storage_info.servo_angle,'f',3) + "\n";
QTextStream out1(&experiment_servo_angle_file);
out1 << write_text;
write_text = QString::number(storage_info.throttle,'d') + "\n";
QTextStream out2(&experiment_throttle_file);
out2 << write_text;
write_text = QString::number(storage_info.voltage,'f',3) + "\n";
QTextStream out3(&experiment_voltage_file);
out3 << write_text;
write_text = QString::number(storage_info.motor_speed,'f',3) + "\n";
QTextStream out4(&experiment_motor_speed_file);
out4 << write_text;
}
}
else
{
qDebug()<<"invalid experiment type " << current_experiment;
}
if( ds_info_queue.isEmpty() == false )
{
ds_storage_info_t ds_info_tmp;
ds_info_tmp = ds_info_queue.dequeue();
write_text = QString::number(ds_info_tmp.voltage_ch[0],'f',3) + " " +
QString::number(ds_info_tmp.voltage_ch[1],'f',3) + " " +
QString::number(ds_info_tmp.voltage_ch[2],'f',3) + " " +
QString::number(ds_info_tmp.voltage_ch[3],'f',3) + "\n";
QTextStream out5(&experiment_digital_sampler_file);
out5 << write_text;
}
}
else if( experiment_status == EXPERIMENT_STATUS_FINISH )
{
experiment_digital_sampler_file.close();
experiment_voltage_file.close();
experiment_servo_angle_file.close();
experiment_motor_speed_file.close();
experiment_throttle_file.close();
experiment_status = EXPERIMENT_STATUS_IDLE;
}
}
/*get sensor info to qml*/
Q_INVOKABLE QVariantMap experiment_info_get(void);
Q_INVOKABLE QVariantMap fc_servo_force_experiment_data_get(void);
Q_INVOKABLE QVariantMap fc_servo_force_dynamic_experiment_data_get(void);
Q_INVOKABLE QVariantMap fc_lift_experiment_data_get(void);
Q_INVOKABLE QString print_info_get(void);
Q_INVOKABLE QVariantMap sensor_info_get(void);
Q_INVOKABLE QVariantMap input_info_get(void);
Q_INVOKABLE QVariantMap expect_info_get(void);
Q_INVOKABLE QVariantList roll_fft_amp_get(void);
Q_INVOKABLE QVariantList pitch_fft_amp_get(void);
Q_INVOKABLE QVariantList yaw_fft_amp_get(void);
Q_INVOKABLE QVariantMap system_initial_info_get(void);
bool flight_status_record_status = false;
bool experiment_pause_flag = false;
protocol_t fc_parse;
/* bsp layer module,parse the communication buffer from flight controler*/
protocol_t ds_parse;
/* bsp layer module,parse the communication buffer from digital sampler*/
serial_port_t fc_port;
/* communication moduler,communicate with flight controler*/
serial_port_t ds_port;
/* communication moduler,communicate with digital sampler*/
sensor_t sensor;
/* sensor module,include sensor information and calculation*/
QQueue<fc_servo_force_storage_info_t> fc_servo_force_info_queue;
QQueue<fc_servoforce_dynamic_storage_info_t> fc_servoforce_dynamic_info_queue;
QQueue<fc_lift_storage_info_t> fc_lift_info_queue;
QQueue<fc_flight_status_info_t> fc_flight_status_info_queue;
/* queue for protocol_servo_force_experiment_fc_info_t to resotre servo force experiment data*/
QQueue<ds_storage_info_t> ds_info_queue;
experiment_type_t current_experiment;
experiment_status_t experiment_status = EXPERIMENT_STATUS_IDLE;
QFile experiment_digital_sampler_file;
QFile experiment_servo_angle_file;
QFile experiment_motor_speed_file;
QFile experiment_throttle_file;
QFile experiment_voltage_file;
QFile gyro_file;
QFile accel_file;
QFile vel_file;
QFile euro_file;
QFile pos_file;
QFile quaternion_file;
QFile voltage_file;
QFile servo_file;
QFile ms_file;
QFile exp_gyro_file;
QFile exp_euro_file;
QFile exp_pos_file;
QFile exp_vel_file;
QFile exp_mspeed_file;
public slots:
signals:
void system_initial_signal(void);
void experiment_status_change_signal(void);
void fc_servo_force_experiment_info_update_signal(void);
void fc_servo_force_dynamic_experiment_info_update_signal(void);
void fc_lift_experiment_info_update_signal(void);
void print_info_update_signal(void);
void sensor_info_update_signal(void);
void input_info_update_signal(void);
void fc_port_status_update_signal(void);
void fc_port_name_list_update_signal(void);
void ds_port_status_update_signal(void);
void ds_port_name_list_update_signal(void);
void roll_fft_data_update_signal(void);
void pitch_fft_data_update_signal(void);
void yaw_fft_data_update_signal(void);
private:
QTimer *parse_timer;
uint32_t parse_period = 1;
uint32_t backend_event[MAX_BACKEND_GROUP_CNT];
qint64 event_tick[BACKEND_EVENT_CNT];
QTimer *system_timer;
uint32_t system_period = 1;
QTimer *fft_timer;
uint32_t fft_period = 200;
const QMap<QString, experiment_type_t> experiment_list_text = {
{"舵片气动力实验", EXPERIMENT_TYPE_SERVO_FORCE},
{"飞行器升力实验", EXPERIMENT_TYPE_MOTOR_LIFT},
{"舵片力动态特性实验", EXPERIMENT_TYPE_SERVO_FORCE_DYNAMIC},
};
const QString file_name_gyro = "gyro_data.txt";
const QString file_name_accel = "accel_data.txt";
const QString file_name_quaternion = "quaternion_data.txt";
const QString file_name_vel = "velocity_data.txt";
const QString file_name_pos = "position_data.txt";
const QString file_name_voltage = "voltage_data.txt";
const QString file_name_euro = "euro_data.txt";
const QString file_name_servo = "servo_data.txt";
const QString file_name_motorspeed = "motor_speed.txt";
const QString file_name_throttle = "throttle.txt";
const QString file_name_digital_sampler = "digital_sample.txt";
const QString file_name_exp_gyro = "exp_gyro.txt";
const QString file_name_exp_euro = "exp_euro.txt";
const QString file_name_exp_pos = "exp_pos.txt";
const QString file_name_exp_vel = "exp_vel.txt";
const QString file_name_exp_mspeed = "exp_mspeed.txt";
void backend_event_loop(void);
};
#endif // RUNNER_H
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化