Skip to content

Commit

Permalink
SITL: use AP_JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
tridge committed Feb 1, 2024
1 parent b8cfd2e commit 66d6a7e
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 1,310 deletions.
4 changes: 1 addition & 3 deletions libraries/SITL/SIM_Aircraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
#include <AP_Terrain/AP_Terrain.h>
#include <AP_Scheduler/AP_Scheduler.h>
#include <AP_BoardConfig/AP_BoardConfig.h>
#if USE_PICOJSON
#include "picojson.h"
#include <AP_JSON/AP_JSON.h>
#include <AP_Filesystem/AP_Filesystem.h>
#endif

using namespace SITL;

Expand Down
4 changes: 0 additions & 4 deletions libraries/SITL/SIM_Aircraft.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@
#include <Filter/Filter.h>
#include "SIM_JSON_Master.h"

#ifndef USE_PICOJSON
#define USE_PICOJSON (CONFIG_HAL_BOARD == HAL_BOARD_SITL || CONFIG_HAL_BOARD == HAL_BOARD_LINUX)
#endif

namespace SITL {

/*
Expand Down
17 changes: 6 additions & 11 deletions libraries/SITL/SIM_Frame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,6 @@ float Frame::get_air_density(float alt_amsl) const
return air_pressure / (ISA_GAS_CONSTANT * (C_TO_KELVIN(model.refTempC)));
}

#if USE_PICOJSON
/*
load frame specific parameters from a json file if available
*/
Expand All @@ -341,7 +340,7 @@ void Frame::load_frame_params(const char *model_json)
if (fname == nullptr) {
AP_HAL::panic("%s failed to load\n", model_json);
}
picojson::value *obj = (picojson::value *)load_json(model_json);
AP_JSON::value *obj = AP_JSON::load_json(model_json);
if (obj == nullptr) {
AP_HAL::panic("%s failed to load\n", model_json);
}
Expand Down Expand Up @@ -386,7 +385,7 @@ void Frame::load_frame_params(const char *model_json)

for (uint8_t i=0; i<ARRAY_SIZE(vars); i++) {
auto v = obj->get(vars[i].label);
if (v.is<picojson::null>()) {
if (v.is<AP_JSON::null>()) {
// use default value
continue;
}
Expand All @@ -409,7 +408,7 @@ void Frame::load_frame_params(const char *model_json)
for (uint8_t j=0; j<12; j++) {
snprintf(label_name, 20, "motor%i_%s", j+1, per_motor_vars[i].label);
auto v = obj->get(label_name);
if (v.is<picojson::null>()) {
if (v.is<AP_JSON::null>()) {
// use default value
continue;
}
Expand All @@ -427,24 +426,22 @@ void Frame::load_frame_params(const char *model_json)
::printf("Loaded model params from %s\n", model_json);
}

void Frame::parse_float(picojson::value val, const char* label, float &param) {
void Frame::parse_float(AP_JSON::value val, const char* label, float &param) {
if (!val.is<double>()) {
AP_HAL::panic("Bad json type for %s: %s", label, val.to_str().c_str());
}
param = val.get<double>();
}

void Frame::parse_vector3(picojson::value val, const char* label, Vector3f &param) {
if (!val.is<picojson::array>() || !val.contains(2) || val.contains(3)) {
void Frame::parse_vector3(AP_JSON::value val, const char* label, Vector3f &param) {
if (!val.is<AP_JSON::value::array>() || !val.contains(2) || val.contains(3)) {
AP_HAL::panic("Bad json type for %s: %s", label, val.to_str().c_str());
}
for (uint8_t j=0; j<3; j++) {
parse_float(val.get(j), label, param[j]);
}
}

#endif

#if AP_SIM_ENABLED

/*
Expand All @@ -455,13 +452,11 @@ void Frame::init(const char *frame_str, Battery *_battery)
model = default_model;
battery = _battery;

#if USE_PICOJSON
const char *colon = strchr(frame_str, ':');
size_t slen = strlen(frame_str);
if (colon != nullptr && slen > 5 && strcmp(&frame_str[slen-5], ".json") == 0) {
load_frame_params(colon+1);
}
#endif
mass = model.mass;

const float drag_force = model.mass * GRAVITY_MSS * tanf(radians(model.refAngle));
Expand Down
13 changes: 3 additions & 10 deletions libraries/SITL/SIM_Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@

#include "SIM_Aircraft.h"
#include "SIM_Motor.h"

#if USE_PICOJSON
#include "picojson.h"
#endif
#include <AP_JSON/AP_JSON.h>

namespace SITL {

Expand Down Expand Up @@ -146,10 +143,8 @@ class Frame {
} default_model;

protected:
#if USE_PICOJSON
// load frame parameters from a json model file
void load_frame_params(const char *model_json);
#endif

// get air density in kg/m^3
float get_air_density(float alt_amsl) const;
Expand All @@ -166,9 +161,7 @@ class Frame {
#endif

// json parsing helpers
#if USE_PICOJSON
void parse_float(picojson::value val, const char* label, float &param);
void parse_vector3(picojson::value val, const char* label, Vector3f &param);
#endif
void parse_float(AP_JSON::value val, const char* label, float &param);
void parse_vector3(AP_JSON::value val, const char* label, Vector3f &param);
};
}
13 changes: 6 additions & 7 deletions libraries/SITL/SIM_XPlane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
#include <AP_HAL/AP_HAL.h>
#include <AP_Filesystem/AP_Filesystem.h>
#include <SRV_Channel/SRV_Channel.h>
#include "picojson.h"
#include <AP_Vehicle/AP_Vehicle_Type.h>

// ignore cast errors in this case to keep complexity down
Expand Down Expand Up @@ -124,7 +123,7 @@ XPlane::XPlane(const char *frame_str) :
/*
add one DRef to list
*/
void XPlane::add_dref(const char *name, DRefType type, const picojson::value &dref)
void XPlane::add_dref(const char *name, DRefType type, const AP_JSON::value &dref)
{
struct DRef *d = new struct DRef;
if (d == nullptr) {
Expand All @@ -149,7 +148,7 @@ void XPlane::add_dref(const char *name, DRefType type, const picojson::value &dr
/*
add one joystick axis to list
*/
void XPlane::add_joyinput(const char *label, JoyType type, const picojson::value &d)
void XPlane::add_joyinput(const char *label, JoyType type, const AP_JSON::value &d)
{
if (strncmp(label, "axis", 4) == 0) {
struct JoyInput *j = new struct JoyInput;
Expand Down Expand Up @@ -180,7 +179,7 @@ void XPlane::add_joyinput(const char *label, JoyType type, const picojson::value
/*
handle a setting
*/
void XPlane::handle_setting(const picojson::value &d)
void XPlane::handle_setting(const AP_JSON::value &d)
{
if (d.contains("debug")) {
dref_debug = d.get("debug").get<double>();
Expand All @@ -205,7 +204,7 @@ bool XPlane::load_dref_map(const char *map_json)
if (fname == nullptr) {
return false;
}
picojson::value *obj = (picojson::value *)load_json(fname);
AP_JSON::value *obj = AP_JSON::load_json(fname);
if (obj == nullptr) {
return false;
}
Expand All @@ -230,8 +229,8 @@ bool XPlane::load_dref_map(const char *map_json)

uint32_t count = 0;
// obtain a const reference to the map, and print the contents
const picojson::value::object& o = obj->get<picojson::object>();
for (picojson::value::object::const_iterator i = o.begin();
const AP_JSON::value::object& o = obj->get<AP_JSON::value::object>();
for (AP_JSON::value::object::const_iterator i = o.begin();
i != o.end();
++i) {
const char *label = i->first.c_str();
Expand Down
8 changes: 4 additions & 4 deletions libraries/SITL/SIM_XPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include <AP_Filesystem/AP_Filesystem.h>

#include "SIM_Aircraft.h"
#include "picojson.h"
#include <AP_JSON/AP_JSON.h>

namespace SITL {

Expand Down Expand Up @@ -126,9 +126,9 @@ class XPlane : public Aircraft {
struct stat map_st;

bool load_dref_map(const char *map_json);
void add_dref(const char *name, DRefType type, const picojson::value &dref);
void add_joyinput(const char *name, JoyType type, const picojson::value &d);
void handle_setting(const picojson::value &d);
void add_dref(const char *name, DRefType type, const AP_JSON::value &dref);
void add_joyinput(const char *name, JoyType type, const AP_JSON::value &d);
void handle_setting(const AP_JSON::value &d);

void check_reload_dref(void);

Expand Down
3 changes: 0 additions & 3 deletions libraries/SITL/examples/EvaluateBatteryModel/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ def build(bld):
source = bld.path.ant_glob('*.cpp')
source.append('../../../../libraries/SITL/SIM_Battery.cpp')
source.append('../../../../libraries/SITL/SIM_Frame.cpp')
source.append('../../../../libraries/SITL/picojson.cpp')

bld.env.DEFINES.append("USE_PICOJSON=1")

bld.ap_program(
use='ap',
Expand Down
3 changes: 0 additions & 3 deletions libraries/SITL/examples/EvaluateMotorModel/wscript
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ def build(bld):
source = bld.path.ant_glob('*.cpp')
source.append('../../../../libraries/SITL/SIM_Motor.cpp')
source.append('../../../../libraries/SITL/SIM_Frame.cpp')
source.append('../../../../libraries/SITL/picojson.cpp')

bld.env.DEFINES.append("USE_PICOJSON=1")

bld.ap_program(
use='ap',
Expand Down
62 changes: 0 additions & 62 deletions libraries/SITL/picojson.cpp

This file was deleted.

Loading

0 comments on commit 66d6a7e

Please sign in to comment.