mavlink: MavlinkStream API simplifyed

This commit is contained in:
Anton Babushkin 2014-07-23 23:10:10 +02:00
parent a65b7aee0b
commit ea2dce3992
4 changed files with 66 additions and 109 deletions

View File

@ -908,7 +908,6 @@ Mavlink::configure_stream(const char *stream_name, const float rate)
/* create new instance */ /* create new instance */
stream = streams_list[i]->new_instance(this); stream = streams_list[i]->new_instance(this);
stream->set_interval(interval); stream->set_interval(interval);
stream->subscribe();
LL_APPEND(_streams, stream); LL_APPEND(_streams, stream);
return OK; return OK;

View File

@ -264,8 +264,8 @@ public:
} }
private: private:
MavlinkOrbSubscription *status_sub; MavlinkOrbSubscription *_status_sub;
MavlinkOrbSubscription *pos_sp_triplet_sub; MavlinkOrbSubscription *_pos_sp_triplet_sub;
/* do not allow top copying this class */ /* do not allow top copying this class */
MavlinkStreamHeartbeat(MavlinkStreamHeartbeat &); MavlinkStreamHeartbeat(MavlinkStreamHeartbeat &);
@ -273,28 +273,22 @@ private:
protected: protected:
explicit MavlinkStreamHeartbeat(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamHeartbeat(Mavlink *mavlink) : MavlinkStream(mavlink),
status_sub(nullptr), _status_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_status))),
pos_sp_triplet_sub(nullptr) _pos_sp_triplet_sub(_mavlink->add_orb_subscription(ORB_ID(position_setpoint_triplet)))
{} {}
void subscribe()
{
status_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_status));
pos_sp_triplet_sub = _mavlink->add_orb_subscription(ORB_ID(position_setpoint_triplet));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct vehicle_status_s status; struct vehicle_status_s status;
struct position_setpoint_triplet_s pos_sp_triplet; struct position_setpoint_triplet_s pos_sp_triplet;
/* always send the heartbeat, independent of the update status of the topics */ /* always send the heartbeat, independent of the update status of the topics */
if (!status_sub->update(&status)) { if (!_status_sub->update(&status)) {
/* if topic update failed fill it with defaults */ /* if topic update failed fill it with defaults */
memset(&status, 0, sizeof(status)); memset(&status, 0, sizeof(status));
} }
if (!pos_sp_triplet_sub->update(&pos_sp_triplet)) { if (!_pos_sp_triplet_sub->update(&pos_sp_triplet)) {
/* if topic update failed fill it with defaults */ /* if topic update failed fill it with defaults */
memset(&pos_sp_triplet, 0, sizeof(pos_sp_triplet)); memset(&pos_sp_triplet, 0, sizeof(pos_sp_triplet));
} }
@ -347,14 +341,10 @@ private:
protected: protected:
explicit MavlinkStreamCommandLong(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamCommandLong(Mavlink *mavlink) : MavlinkStream(mavlink),
_cmd_sub(nullptr), _cmd_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_command))),
_cmd_time(0) _cmd_time(0)
{} {}
void subscribe() {
_cmd_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_command));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct vehicle_command_s cmd; struct vehicle_command_s cmd;
@ -411,7 +401,7 @@ public:
} }
private: private:
MavlinkOrbSubscription *status_sub; MavlinkOrbSubscription *_status_sub;
/* do not allow top copying this class */ /* do not allow top copying this class */
MavlinkStreamSysStatus(MavlinkStreamSysStatus &); MavlinkStreamSysStatus(MavlinkStreamSysStatus &);
@ -419,19 +409,14 @@ private:
protected: protected:
explicit MavlinkStreamSysStatus(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamSysStatus(Mavlink *mavlink) : MavlinkStream(mavlink),
status_sub(nullptr) _status_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_status)))
{} {}
void subscribe()
{
status_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_status));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct vehicle_status_s status; struct vehicle_status_s status;
if (status_sub->update(&status)) { if (_status_sub->update(&status)) {
mavlink_sys_status_t msg; mavlink_sys_status_t msg;
msg.onboard_control_sensors_present = status.onboard_control_sensors_present; msg.onboard_control_sensors_present = status.onboard_control_sensors_present;
@ -483,13 +468,13 @@ public:
} }
private: private:
MavlinkOrbSubscription *sensor_sub; MavlinkOrbSubscription *_sensor_sub;
uint64_t sensor_time; uint64_t _sensor_time;
uint64_t accel_timestamp; uint64_t _accel_timestamp;
uint64_t gyro_timestamp; uint64_t _gyro_timestamp;
uint64_t mag_timestamp; uint64_t _mag_timestamp;
uint64_t baro_timestamp; uint64_t _baro_timestamp;
/* do not allow top copying this class */ /* do not allow top copying this class */
MavlinkStreamHighresIMU(MavlinkStreamHighresIMU &); MavlinkStreamHighresIMU(MavlinkStreamHighresIMU &);
@ -497,48 +482,43 @@ private:
protected: protected:
explicit MavlinkStreamHighresIMU(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamHighresIMU(Mavlink *mavlink) : MavlinkStream(mavlink),
sensor_sub(nullptr), _sensor_sub(_mavlink->add_orb_subscription(ORB_ID(sensor_combined))),
sensor_time(0), _sensor_time(0),
accel_timestamp(0), _accel_timestamp(0),
gyro_timestamp(0), _gyro_timestamp(0),
mag_timestamp(0), _mag_timestamp(0),
baro_timestamp(0) _baro_timestamp(0)
{} {}
void subscribe()
{
sensor_sub = _mavlink->add_orb_subscription(ORB_ID(sensor_combined));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct sensor_combined_s sensor; struct sensor_combined_s sensor;
if (sensor_sub->update(&sensor_time, &sensor)) { if (_sensor_sub->update(&_sensor_time, &sensor)) {
uint16_t fields_updated = 0; uint16_t fields_updated = 0;
if (accel_timestamp != sensor.accelerometer_timestamp) { if (_accel_timestamp != sensor.accelerometer_timestamp) {
/* mark first three dimensions as changed */ /* mark first three dimensions as changed */
fields_updated |= (1 << 0) | (1 << 1) | (1 << 2); fields_updated |= (1 << 0) | (1 << 1) | (1 << 2);
accel_timestamp = sensor.accelerometer_timestamp; _accel_timestamp = sensor.accelerometer_timestamp;
} }
if (gyro_timestamp != sensor.timestamp) { if (_gyro_timestamp != sensor.timestamp) {
/* mark second group dimensions as changed */ /* mark second group dimensions as changed */
fields_updated |= (1 << 3) | (1 << 4) | (1 << 5); fields_updated |= (1 << 3) | (1 << 4) | (1 << 5);
gyro_timestamp = sensor.timestamp; _gyro_timestamp = sensor.timestamp;
} }
if (mag_timestamp != sensor.magnetometer_timestamp) { if (_mag_timestamp != sensor.magnetometer_timestamp) {
/* mark third group dimensions as changed */ /* mark third group dimensions as changed */
fields_updated |= (1 << 6) | (1 << 7) | (1 << 8); fields_updated |= (1 << 6) | (1 << 7) | (1 << 8);
mag_timestamp = sensor.magnetometer_timestamp; _mag_timestamp = sensor.magnetometer_timestamp;
} }
if (baro_timestamp != sensor.baro_timestamp) { if (_baro_timestamp != sensor.baro_timestamp) {
/* mark last group dimensions as changed */ /* mark last group dimensions as changed */
fields_updated |= (1 << 9) | (1 << 11) | (1 << 12); fields_updated |= (1 << 9) | (1 << 11) | (1 << 12);
baro_timestamp = sensor.baro_timestamp; _baro_timestamp = sensor.baro_timestamp;
} }
mavlink_highres_imu_t msg; mavlink_highres_imu_t msg;
@ -594,8 +574,8 @@ public:
} }
private: private:
MavlinkOrbSubscription *att_sub; MavlinkOrbSubscription *_att_sub;
uint64_t att_time; uint64_t _att_time;
/* do not allow top copying this class */ /* do not allow top copying this class */
MavlinkStreamAttitude(MavlinkStreamAttitude &); MavlinkStreamAttitude(MavlinkStreamAttitude &);
@ -604,20 +584,15 @@ private:
protected: protected:
explicit MavlinkStreamAttitude(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamAttitude(Mavlink *mavlink) : MavlinkStream(mavlink),
att_sub(nullptr), _att_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_attitude))),
att_time(0) _att_time(0)
{} {}
void subscribe()
{
att_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_attitude));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct vehicle_attitude_s att; struct vehicle_attitude_s att;
if (att_sub->update(&att_time, &att)) { if (_att_sub->update(&_att_time, &att)) {
mavlink_attitude_t msg; mavlink_attitude_t msg;
msg.time_boot_ms = att.timestamp / 1000; msg.time_boot_ms = att.timestamp / 1000;
@ -663,8 +638,8 @@ public:
} }
private: private:
MavlinkOrbSubscription *att_sub; MavlinkOrbSubscription *_att_sub;
uint64_t att_time; uint64_t _att_time;
/* do not allow top copying this class */ /* do not allow top copying this class */
MavlinkStreamAttitudeQuaternion(MavlinkStreamAttitudeQuaternion &); MavlinkStreamAttitudeQuaternion(MavlinkStreamAttitudeQuaternion &);
@ -672,20 +647,15 @@ private:
protected: protected:
explicit MavlinkStreamAttitudeQuaternion(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamAttitudeQuaternion(Mavlink *mavlink) : MavlinkStream(mavlink),
att_sub(nullptr), _att_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_attitude))),
att_time(0) _att_time(0)
{} {}
void subscribe()
{
att_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_attitude));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct vehicle_attitude_s att; struct vehicle_attitude_s att;
if (att_sub->update(&att_time, &att)) { if (_att_sub->update(&_att_time, &att)) {
mavlink_attitude_quaternion_t msg; mavlink_attitude_quaternion_t msg;
msg.time_boot_ms = att.timestamp / 1000; msg.time_boot_ms = att.timestamp / 1000;
@ -733,20 +703,20 @@ public:
} }
private: private:
MavlinkOrbSubscription *att_sub; MavlinkOrbSubscription *_att_sub;
uint64_t att_time; uint64_t _att_time;
MavlinkOrbSubscription *pos_sub; MavlinkOrbSubscription *_pos_sub;
uint64_t pos_time; uint64_t _pos_time;
MavlinkOrbSubscription *armed_sub; MavlinkOrbSubscription *_armed_sub;
uint64_t armed_time; uint64_t _armed_time;
MavlinkOrbSubscription *act_sub; MavlinkOrbSubscription *_act_sub;
uint64_t act_time; uint64_t _act_time;
MavlinkOrbSubscription *airspeed_sub; MavlinkOrbSubscription *_airspeed_sub;
uint64_t airspeed_time; uint64_t _airspeed_time;
/* do not allow top copying this class */ /* do not allow top copying this class */
MavlinkStreamVFRHUD(MavlinkStreamVFRHUD &); MavlinkStreamVFRHUD(MavlinkStreamVFRHUD &);
@ -754,27 +724,18 @@ private:
protected: protected:
explicit MavlinkStreamVFRHUD(Mavlink *mavlink) : MavlinkStream(mavlink), explicit MavlinkStreamVFRHUD(Mavlink *mavlink) : MavlinkStream(mavlink),
att_sub(nullptr), _att_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_attitude))),
att_time(0), _att_time(0),
pos_sub(nullptr), _pos_sub(_mavlink->add_orb_subscription(ORB_ID(vehicle_global_position))),
pos_time(0), _pos_time(0),
armed_sub(nullptr), _armed_sub(_mavlink->add_orb_subscription(ORB_ID(actuator_armed))),
armed_time(0), _armed_time(0),
act_sub(nullptr), _act_sub(_mavlink->add_orb_subscription(ORB_ID(actuator_controls_0))),
act_time(0), _act_time(0),
airspeed_sub(nullptr), _airspeed_sub(_mavlink->add_orb_subscription(ORB_ID(airspeed))),
airspeed_time(0) _airspeed_time(0)
{} {}
void subscribe()
{
att_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_attitude));
pos_sub = _mavlink->add_orb_subscription(ORB_ID(vehicle_global_position));
armed_sub = _mavlink->add_orb_subscription(ORB_ID(actuator_armed));
act_sub = _mavlink->add_orb_subscription(ORB_ID(actuator_controls_0));
airspeed_sub = _mavlink->add_orb_subscription(ORB_ID(airspeed));
}
void send(const hrt_abstime t) void send(const hrt_abstime t)
{ {
struct vehicle_attitude_s att; struct vehicle_attitude_s att;
@ -783,11 +744,11 @@ protected:
struct actuator_controls_s act; struct actuator_controls_s act;
struct airspeed_s airspeed; struct airspeed_s airspeed;
bool updated = att_sub->update(&att_time, &att); bool updated = _att_sub->update(&_att_time, &att);
updated |= pos_sub->update(&pos_time, &pos); updated |= _pos_sub->update(&_pos_time, &pos);
updated |= armed_sub->update(&armed_time, &armed); updated |= _armed_sub->update(&_armed_time, &armed);
updated |= act_sub->update(&act_time, &act); updated |= _act_sub->update(&_act_time, &act);
updated |= airspeed_sub->update(&airspeed_time, &airspeed); updated |= _airspeed_sub->update(&_airspeed_time, &airspeed);
if (updated) { if (updated) {
mavlink_vfr_hud_t msg; mavlink_vfr_hud_t msg;

View File

@ -109,8 +109,6 @@ private:
protected: protected:
explicit MavlinkParametersManager(Mavlink *mavlink); explicit MavlinkParametersManager(Mavlink *mavlink);
void subscribe() {}
void send(const hrt_abstime t); void send(const hrt_abstime t);
void send_param(param_t param); void send_param(param_t param);

View File

@ -75,7 +75,6 @@ public:
int update(const hrt_abstime t); int update(const hrt_abstime t);
static MavlinkStream *new_instance(const Mavlink *mavlink); static MavlinkStream *new_instance(const Mavlink *mavlink);
static const char *get_name_static(); static const char *get_name_static();
virtual void subscribe() = 0;
virtual const char *get_name() const = 0; virtual const char *get_name() const = 0;
virtual uint8_t get_id() = 0; virtual uint8_t get_id() = 0;