The Plane Project [1] – 使用串口位运算传输飞行数据 / 控制请求

怎么传输飞行数据和控制请求是一个值得探究的问题。一开始我想用JSON,用Arduino的aJSON库和C#的写出了一些乱七八糟的代码,最后发现数据量、延时直接上天【500ms】,程序还经常跑飞,再加上无线模块的串口速率【19200】就更慢了ww,于是脑洞大开想到了位运算处理,将延时降到了【300ms】。

这是我最初使用JSON的Arduino核心代码:

void process_json() {
aJsonObject *temp_ex;

temp = aJson.getObjectItem(control_request, "Flaps");
if (temp) {
temp_ex = aJson.getObjectItem(temp, "l");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
flaps.l = temp_ex -> valueint;

temp_ex = aJson.getObjectItem(temp, "r");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
flaps.r = temp_ex -> valueint;
}

temp = aJson.getObjectItem(control_request, "Slaps");
if (temp) {
temp_ex = aJson.getObjectItem(temp, "l");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
slaps.l = temp_ex -> valueint;

temp_ex = aJson.getObjectItem(temp, "r");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
slaps.r = temp_ex -> valueint;
}

temp = aJson.getObjectItem(control_request, "Thro");
if (temp)
if (temp -> type == aJson_Int) throttle = temp -> valueint;

temp = aJson.getObjectItem(control_request, "SBak");
if (temp)
if (temp -> type == aJson_Int)
speed_brakes = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Rud");
if (temp)
if (temp -> type == aJson_Int)
rudder = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Ele");
if (temp)
if (temp -> type == aJson_Int)
elevator = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Trim");
if (temp)
if (temp -> type == aJson_Int)
trim = temp -> valueint;

temp = aJson.getObjectItem(control_request, "Lig");
if (temp) {
temp_ex = aJson.getObjectItem(temp, "l1");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
lights[1] = temp_ex -> valuebool;

temp_ex = aJson.getObjectItem(temp, "l2");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
lights[2] = temp_ex -> valuebool;

temp_ex = aJson.getObjectItem(temp, "l3");
if (temp_ex)
if (temp_ex -> type == aJson_Int)
lights[3] = temp_ex -> valuebool;
}
}

void create_json() {

aJson.deleteItem(flight_data);
flight_data = aJson.createObject();

aJson.addNumberToObject(flight_data, "MSpd", motor_speed);
aJson.addNumberToObject(flight_data, "ASpd", air_speed);
aJson.addNumberToObject(flight_data, "GSpd", ground_speed);
aJson.addNumberToObject(flight_data, "Alt", altitude);
aJson.addNumberToObject(flight_data, "Hea", heading);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "Lat", loc.lon);
aJson.addNumberToObject(temp, "Lou", loc.lat);
aJson.addItemToObject(flight_data, "Loc", temp);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "x", acc.x);
aJson.addNumberToObject(temp, "y", acc.y);
aJson.addNumberToObject(temp, "z", acc.z);
aJson.addItemToObject(flight_data, "Acc", temp);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "x", gyro.x);
aJson.addNumberToObject(temp, "y", gyro.y);
aJson.addNumberToObject(temp, "z", gyro.z);
aJson.addItemToObject(flight_data, "Gyro", temp);

temp = aJson.createObject();
aJson.addNumberToObject(temp, "x", angle.x);
aJson.addNumberToObject(temp, "y", angle.y);
aJson.addNumberToObject(temp, "z", angle.z);
aJson.addItemToObject(flight_data, "Angle", temp);

aJson.addNumberToObject(flight_data, "BtVolt", battery_voltage);

}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *