Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: SensorHub #6388

Closed
wants to merge 37 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
df0f368
AP_SensorHub: Adds Protocol.
mayanez Jul 12, 2017
80ce931
Build: AP_SensorHub Initial Build Support
mayanez Jul 12, 2017
5e29e3e
DataFlash: Adds AP_SensorHub Message Structure
mayanez Jul 25, 2017
eb3daf1
AP_HAL: Adds raw_micros().
mayanez Aug 21, 2017
4b952e7
AP_HAL_Linux: Implements HAL raw_micros()
mayanez Aug 21, 2017
e49e00f
AP_HAL: Adds AP_SensorHub Device Type
mayanez Aug 11, 2017
97816a8
AP_HAL: Adds claim() to UARTDriver.
mayanez Aug 11, 2017
ef99d51
AP_HAL_SITL: Adds claim() to UART Driver
mayanez Aug 11, 2017
afdad29
AP_HAL_PX4: Adds claim() to UART Driver
mayanez Aug 11, 2017
77a9f69
AP_HAL_Linux: Adds claim() to UART Driver
mayanez Aug 11, 2017
b42a225
AP_SensorHub: Adds Basic SensorHub Library.
mayanez Jul 12, 2017
d8e6f9a
AP_SensorHub: Adds IO Implementations.
mayanez Jul 12, 2017
80b1dc1
AP_Baro: Adds AP_SensorHub to Backend.
mayanez Jul 12, 2017
0d8d45f
AP_Baro: Adds AP_SensorHub Backend.
mayanez Jul 12, 2017
c70bf2b
AP_Compass: Adds AP_SensorHub to Backend.
mayanez Jul 12, 2017
b911c9d
AP_Compass: Adds AP_SensorHub Backend.
mayanez Jul 13, 2017
d02cd22
AP_InertialSensor: Adds AP_SensorHub to Backend.
mayanez Jul 13, 2017
77cd9e9
AP_InertialSensor: Adds AP_SensorHub Backend.
mayanez Jul 13, 2017
849c875
AP_GPS: Adds AP_SensorHub to Backend.
mayanez Jul 13, 2017
ddcfbfd
AP_GPS: Adds AP_SensorHub Backend.
mayanez Jul 13, 2017
375e9b7
AP_SerialManager: Adds AP_SensorHub as a protocol.
mayanez Jul 13, 2017
9aa2690
AP_HAL: Adds AP_SensorHub Sink Sensors.
mayanez Jul 13, 2017
536f0fa
AP_InertialSensor: Make initializing backends optional.
mayanez Jul 13, 2017
493f743
AP_InertialSensor: Refactor Singleton implementation.
mayanez Jul 13, 2017
7063faa
AP_InertialSensor: Makes _add_backend public.
mayanez Jul 13, 2017
867ae00
AP_GPS: Update init() to make serial protocol an option.
mayanez Jul 13, 2017
9229ce2
AP_Compass: Makes _add_backend public.
mayanez Jul 13, 2017
f3a179b
AP_Baro: Makes _add_backend public.
mayanez Jul 13, 2017
7dc4b01
AP_Baro: Adds ground_temp_init()
mayanez Jul 13, 2017
0edc927
AP_HAL_SITL: Adds AP_SensorHead - Source Simulation.
mayanez Jul 13, 2017
4e2cd01
Copter: Integrates AP_SensorHub.
mayanez Jul 13, 2017
6d50ca4
Tools: Adds Copter Params for AP_SensorHub SITL Integration.
mayanez Jul 13, 2017
b42c786
AP_SensorHub: Adds lightweight example Sink.
mayanez Aug 11, 2017
bc749b0
AP_HAL: Adds SensorHub Sink support for Navio2
mayanez Aug 15, 2017
3603849
GCS_MAVLink: Check if GCS is not null.
mayanez Aug 21, 2017
de901c5
SensorHub_Source: Adds AP_SensorHub Source as vehicle type
mayanez Aug 15, 2017
9e97818
Tools: Adds Travis CI support for building SITL SensorHub
mayanez Aug 23, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ matrix:
env: CI_BUILD_TARGET="px4-v4"
- compiler: "gcc"
env: CI_BUILD_TARGET="sitltest"
- compiler: "gcc"
env: CI_BUILD_TARGET="sitltest-shub"
4 changes: 4 additions & 0 deletions ArduCopter/Copter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const AP_HAL::HAL& hal = AP_HAL::get_HAL();
*/
Copter::Copter(void) :
DataFlash{FIRMWARE_STRING, g.log_bitmask},
gps_serial_protocol(AP_SerialManager::SerialProtocol_GPS),
flight_modes(&g.flight_mode1),
mission(ahrs,
FUNCTOR_BIND_MEMBER(&Copter::start_command, bool, const AP_Mission::Mission_Command &),
Expand Down Expand Up @@ -104,6 +105,9 @@ Copter::Copter(void) :
in_mavlink_delay(false),
gcs_out_of_time(false),
param_loader(var_info)
#if HAL_SENSORHUB_ENABLED
,shub(AP_SensorHub::init_instance())
#endif
{
memset(&current_loc, 0, sizeof(current_loc));

Expand Down
20 changes: 19 additions & 1 deletion ArduCopter/Copter.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@
#include <SITL/SITL.h>
#endif

#if HAL_SENSORHUB_ENABLED
#include <AP_SensorHub/AP_SensorHub.h>
#endif


class Copter : public AP_HAL::HAL::Callbacks {
public:
Expand Down Expand Up @@ -193,6 +197,7 @@ class Copter : public AP_HAL::HAL::Callbacks {
DataFlash_Class DataFlash;

AP_GPS gps;
AP_SerialManager::SerialProtocol gps_serial_protocol;

// flight modes convenience array
AP_Int8 *flight_modes;
Expand Down Expand Up @@ -220,6 +225,11 @@ class Copter : public AP_HAL::HAL::Callbacks {

#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
SITL::SITL sitl;

AP_Baro baro_sitl;
Compass compass_sitl;
AP_InertialSensor ins_sitl;
AP_GPS gps_sitl;
#endif

// Mission library
Expand Down Expand Up @@ -641,7 +651,11 @@ class Copter : public AP_HAL::HAL::Callbacks {

// set when we are upgrading parameters from 3.4
bool upgrading_frame_params;


#if HAL_SENSORHUB_ENABLED
AP_SensorHub *shub;
#endif

static const AP_Scheduler::Task scheduler_tasks[];
static const AP_Param::Info var_info[];
static const struct LogStructure log_structure[];
Expand Down Expand Up @@ -1160,6 +1174,10 @@ class Copter : public AP_HAL::HAL::Callbacks {
void dataflash_periodic(void);
void accel_cal_update(void);

#if HAL_SENSORHUB_ENABLED
void setup_shub();
#endif

public:
void mavlink_delay_cb();
void failsafe_check();
Expand Down
4 changes: 4 additions & 0 deletions ArduCopter/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,6 +752,10 @@ const AP_Param::Info Copter::var_info[] = {

#if CONFIG_HAL_BOARD == HAL_BOARD_SITL
GOBJECT(sitl, "SIM_", SITL::SITL),
GOBJECT(baro_sitl, "SG_", AP_Baro),
GOBJECT(compass_sitl, "SC_", Compass),
GOBJECT(ins_sitl, "SI_", AP_InertialSensor),
GOBJECT(gps_sitl, "SP_", AP_GPS),
#endif

// @Group: GND_
Expand Down
5 changes: 5 additions & 0 deletions ArduCopter/Parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,11 @@ class Parameters {

// 254,255: reserved

k_param_baro_sitl = 300,
k_param_compass_sitl,
k_param_ins_sitl,
k_param_gps_sitl,

// the k_param_* space is 9-bits in size
// 511: reserved
};
Expand Down
22 changes: 21 additions & 1 deletion ArduCopter/system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ void Copter::init_ardupilot()
// initialise serial ports
serial_manager.init();

#if HAL_SENSORHUB_ENABLED
setup_shub();
#endif

// setup first port early to allow BoardConfig to report errors
gcs_chan[0].setup_uart(serial_manager, AP_SerialManager::SerialProtocol_MAVLink, 0);

Expand Down Expand Up @@ -209,7 +213,8 @@ void Copter::init_ardupilot()

// Do GPS init
gps.set_log_gps_bit(MASK_LOG_GPS);
gps.init(serial_manager);

gps.init(serial_manager, gps_serial_protocol);

init_compass();

Expand Down Expand Up @@ -333,6 +338,10 @@ void Copter::init_ardupilot()

// flag that initialisation has completed
ap.initialised = true;

#if HAL_SENSORHUB_ENABLED
shub->start();
#endif
}


Expand Down Expand Up @@ -722,3 +731,14 @@ void Copter::allocate_motors(void)
// upgrade parameters. This must be done after allocating the objects
convert_pid_parameters();
}

#if HAL_SENSORHUB_ENABLED
void Copter::setup_shub()
{
#if SENSORHUB_MODE == SENSORHUB_SINK
gps_serial_protocol = AP_SerialManager::SerialProtocol_SENSORHUB;
#endif

shub->init(serial_manager);
}
#endif
1 change: 1 addition & 0 deletions SensorHub/APM_Config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
26 changes: 26 additions & 0 deletions SensorHub/Parameters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <AP_Common/AP_Common.h>

class Parameters {
public:
static const uint16_t k_format_version = 1;
static const uint16_t k_software_type = 5;

enum {
k_param_format_version = 0,
k_param_software_type,
k_param_scheduler,
k_param_gps,
k_param_barometer,
k_param_compass,
k_param_ins,
k_param_serial_manager,
k_param_BoardConfig,
};

AP_Int16 format_version;
AP_Int16 software_type;
};

extern const AP_Param::Info var_info[];
131 changes: 131 additions & 0 deletions SensorHub/SensorHub.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
#include "SensorHub.h"
#include <AP_GPS/GPS_Backend.h>

const AP_HAL::HAL& hal = AP_HAL::get_HAL();

#if HAL_SENSORHUB_ENABLED

SensorHub_Source sensorhub_source;

#define SCHED_TASK(func, rate_hz, max_time_micros) SCHED_TASK_CLASS(SensorHub_Source, &sensorhub_source, func, rate_hz, max_time_micros)
#define GSCALAR(v, name, def) { sensorhub_source.g.v.vtype, name, Parameters::k_param_ ## v, &sensorhub_source.g.v, {def_value : def} }
#define GOBJECT(v, name, class) { AP_PARAM_GROUP, name, Parameters::k_param_ ## v, (const void *)&sensorhub_source.v, {group_info : class::var_info} }

const AP_Scheduler::Task SensorHub_Source::scheduler_tasks[] = {
SCHED_TASK(update_barometer, 100, 100),
SCHED_TASK(update_gps, 50, 200)
};

const AP_Param::Info SensorHub_Source::var_info[] = {
// @Param: FORMAT_VERSION
// @DisplayName: Eeprom format version number
// @Description: This value is incremented when changes are made to the eeprom format
// @User: Advanced
GSCALAR(format_version, "FORMAT_VERSION", 0),

// @Param: SYSID_SW_TYPE
// @DisplayName: Software Type
// @Description: This is used by the ground station to recognise the software type (eg ArduPlane vs ArduCopter)
// @Values: 0:ArduPlane,4:AntennaTracker,10:Copter,20:Rover
// @User: Advanced
// @ReadOnly: True
GSCALAR(software_type, "SYSID_SW_TYPE", Parameters::k_software_type),


GOBJECT(compass, "COMPASS_", Compass),
GOBJECT(ins, "INS_", AP_InertialSensor),
GOBJECT(BoardConfig, "BRD_", AP_BoardConfig),
GOBJECT(barometer, "GND_", AP_Baro),
GOBJECT(gps, "GPS_", AP_GPS),
GOBJECT(scheduler, "SCHED_", AP_Scheduler),
AP_VAREND
};

SensorHub_Source::SensorHub_Source(void) :
shub(AP_SensorHub::init_instance())
{}

void SensorHub_Source::update_barometer()
{
barometer.update();
}

void SensorHub_Source::update_gps()
{
gps.update();
}

void SensorHub_Source::load_parameters()
{
if (!g.format_version.load() ||
g.format_version != Parameters::k_format_version) {

// erase all parameters
hal.console->printf("Firmware change: erasing EEPROM...\n");
AP_Param::erase_all();

// save the current format version
g.format_version.set_and_save(Parameters::k_format_version);
hal.console->printf("done.\n");
}

uint32_t before = AP_HAL::micros();
// Load all auto-loaded EEPROM variables
AP_Param::load_all();
hal.console->printf("load_all took %luus\n", (unsigned long)(AP_HAL::micros() - before));
}
void SensorHub_Source::setup()
{
AP_Param::setup_sketch_defaults();
serial_manager.init_console();
serial_manager.init();
load_parameters();
BoardConfig.init();
shub->init(serial_manager);

barometer.init();
barometer.calibrate();

compass.init();

gps.init(serial_manager, AP_SerialManager::SerialProtocol_GPS);
gps.update();

bool gpsInitialized = false;
do {
for (int i = 0; i < GPS_MAX_RECEIVERS; i++) {
auto driver = gps.get_driver(i);
if (driver) {
driver->setSensorHub(shub);
gpsInitialized = true;
hal.console->printf("Initialized GPS\n");
}
}
gps.update();
hal.scheduler->delay(1);
} while (!gpsInitialized);

ins.init(scheduler.get_loop_rate_hz());
shub->start();

hal.console->printf("Initializing Scheduler...\n");
scheduler.init(&scheduler_tasks[0], ARRAY_SIZE(scheduler_tasks));
}

void SensorHub_Source::loop()
{
ins.wait_for_sample();
ins.update();

auto timer = AP_HAL::micros();
scheduler.tick();
auto time_available = (timer + MAIN_LOOP_MICROS) - AP_HAL::micros();
scheduler.run(time_available > MAIN_LOOP_MICROS ? 0u : time_available);
}

AP_HAL_MAIN_CALLBACKS(&sensorhub_source);
#else
void setup() {}
void loop() {}
AP_HAL_MAIN();
#endif
65 changes: 65 additions & 0 deletions SensorHub/SensorHub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#pragma once

#include <AP_Common/AP_Common.h>
#include <AP_HAL/AP_HAL.h>
#include <AP_Param/AP_Param.h>
#include <StorageManager/StorageManager.h>
#include <AP_GPS/AP_GPS.h>
#include <AP_Baro/AP_Baro.h>
#include <AP_Compass/AP_Compass.h>
#include <AP_Math/AP_Math.h>
#include <AP_InertialSensor/AP_InertialSensor.h>
#include <AP_AccelCal/AP_AccelCal.h>
#include <AP_AHRS/AP_AHRS.h>

#include <AP_SerialManager/AP_SerialManager.h>
#include <AP_Declination/AP_Declination.h>
#include <DataFlash/DataFlash.h>
#include <AP_Scheduler/AP_Scheduler.h>

#include <AP_Vehicle/AP_Vehicle.h>

#include <AP_BoardConfig/AP_BoardConfig.h>

#include <AP_SensorHub/AP_SensorHub.h>

#include "defines.h"
#include "config.h"
#include "Parameters.h"

#if HAL_SENSORHUB_ENABLED
class SensorHub_Source : public AP_HAL::HAL::Callbacks {
public:
friend class Parameters;

SensorHub_Source(void);

void setup() override;
void loop() override;

private:
Parameters g;
AP_Scheduler scheduler;
AP_GPS gps;
AP_Baro barometer;
Compass compass;
AP_InertialSensor ins;

AP_SerialManager serial_manager;
AP_BoardConfig BoardConfig;

AP_Param param_loader{var_info};

static const AP_Param::Info var_info[];
static const AP_Scheduler::Task scheduler_tasks[];

AP_SensorHub *shub;

public:
void update_barometer();
void update_gps();
void load_parameters();
};

extern SensorHub_Source sensorhub_source;
#endif
9 changes: 9 additions & 0 deletions SensorHub/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include "defines.h"
#include "APM_Config.h"

// run at 400Hz on all systems
# define MAIN_LOOP_RATE 400
# define MAIN_LOOP_SECONDS 0.0025f
# define MAIN_LOOP_MICROS 2500
1 change: 1 addition & 0 deletions SensorHub/defines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
Loading