2018-03-07 21:25:44 -04:00
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
simple vicon simulator class
|
|
|
|
|
|
|
|
XKFR
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SIM_Vicon.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
using namespace SITL;
|
|
|
|
|
|
|
|
Vicon::Vicon()
|
|
|
|
{
|
|
|
|
int tmp[2];
|
2018-03-28 22:51:46 -03:00
|
|
|
if (pipe(tmp) == -1) {
|
|
|
|
AP_HAL::panic("pipe() failed");
|
|
|
|
}
|
2018-03-07 21:25:44 -04:00
|
|
|
fd_my_end = tmp[1];
|
|
|
|
fd_their_end = tmp[0];
|
|
|
|
|
|
|
|
// make sure we don't screw the simulation up by blocking:
|
|
|
|
fcntl(fd_my_end, F_SETFL, fcntl(fd_my_end, F_GETFL, 0) | O_NONBLOCK);
|
|
|
|
fcntl(fd_their_end, F_SETFL, fcntl(fd_their_end, F_GETFL, 0) | O_NONBLOCK);
|
2018-03-31 06:05:23 -03:00
|
|
|
|
|
|
|
if (!valid_channel(mavlink_ch)) {
|
|
|
|
AP_HAL::panic("Invalid mavlink channel");
|
|
|
|
}
|
2018-03-07 21:25:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void Vicon::maybe_send_heartbeat()
|
|
|
|
{
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
if (now - last_heartbeat_ms < 100) {
|
|
|
|
// we only provide a heartbeat every so often
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
last_heartbeat_ms = now;
|
|
|
|
|
|
|
|
mavlink_message_t msg;
|
|
|
|
mavlink_msg_heartbeat_pack(system_id,
|
|
|
|
component_id,
|
|
|
|
&msg,
|
|
|
|
MAV_TYPE_GCS,
|
|
|
|
MAV_AUTOPILOT_INVALID,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Vicon::update_vicon_position_estimate(const Location &loc,
|
|
|
|
const Vector3f &position,
|
|
|
|
const Quaternion &attitude)
|
|
|
|
{
|
|
|
|
const uint32_t now = AP_HAL::millis();
|
|
|
|
|
|
|
|
if (now - vicon.last_observation_ms < vicon.observation_interval_ms) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
obs_elements new_obs = {
|
|
|
|
now,
|
|
|
|
position,
|
|
|
|
attitude
|
|
|
|
};
|
|
|
|
vicon.last_observation_ms = now;
|
|
|
|
|
|
|
|
obs_elements observation;
|
2018-03-25 23:54:05 -03:00
|
|
|
if (_sitl->vicon_observation_history_length == 0) {
|
2018-03-07 21:25:44 -04:00
|
|
|
// no delay
|
|
|
|
observation = new_obs;
|
|
|
|
} else {
|
2018-03-25 23:54:05 -03:00
|
|
|
vicon.observation_history->push(new_obs);
|
2018-03-07 21:25:44 -04:00
|
|
|
|
2018-03-25 23:54:05 -03:00
|
|
|
if (vicon.observation_history->space() != 0) {
|
2018-03-07 21:25:44 -04:00
|
|
|
::fprintf(stderr, "Insufficient delay\n");
|
|
|
|
return;
|
|
|
|
}
|
2018-03-25 23:54:05 -03:00
|
|
|
if (!vicon.observation_history->pop(observation)) {
|
2018-03-07 21:25:44 -04:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float roll;
|
|
|
|
float pitch;
|
|
|
|
float yaw;
|
|
|
|
observation.attitude.to_euler(roll, pitch, yaw);
|
|
|
|
|
|
|
|
mavlink_message_t msg;
|
2018-03-31 06:05:23 -03:00
|
|
|
const uint16_t n = mavlink_msg_vicon_position_estimate_pack_chan(
|
|
|
|
system_id,
|
|
|
|
component_id,
|
|
|
|
mavlink_ch,
|
|
|
|
&msg,
|
|
|
|
observation.time_ms*1000,
|
|
|
|
observation.position.x,
|
|
|
|
observation.position.y,
|
|
|
|
observation.position.z,
|
|
|
|
roll,
|
|
|
|
pitch,
|
|
|
|
yaw);
|
|
|
|
|
|
|
|
// ::fprintf(stderr, "Vicon: %u: writing pos=(%03.03f %03.03f %03.03f) att=(%01.03f %01.03f %01.03f)\n", observation.time_ms, observation.position.x, observation.position.y, observation.position.z, roll, pitch, yaw);
|
|
|
|
if (::write(fd_my_end, (void*)&msg, n) != n) {
|
2018-03-07 21:25:44 -04:00
|
|
|
::fprintf(stderr, "Vicon: write failure\n");
|
|
|
|
// abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-25 23:54:05 -03:00
|
|
|
bool Vicon::init_sitl_pointer()
|
|
|
|
{
|
|
|
|
if (_sitl == nullptr) {
|
|
|
|
_sitl = (SITL *)AP_Param::find_object("SIM_");
|
|
|
|
if (_sitl == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (_sitl->vicon_observation_history_length > 0 &&
|
|
|
|
vicon.observation_history == nullptr) {
|
|
|
|
const uint8_t maxlen = 100;
|
|
|
|
if (_sitl->vicon_observation_history_length > maxlen) {
|
|
|
|
::fprintf(stderr, "Clamping history length to %u", maxlen);
|
|
|
|
_sitl->vicon_observation_history_length = maxlen;
|
|
|
|
}
|
|
|
|
vicon.observation_history = new ObjectArray<obs_elements>(_sitl->vicon_observation_history_length);
|
|
|
|
if (vicon.observation_history == nullptr) {
|
|
|
|
AP_HAL::panic("Failed to allocate history");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:25:44 -04:00
|
|
|
/*
|
|
|
|
update vicon sensor state
|
|
|
|
*/
|
|
|
|
void Vicon::update(const Location &loc, const Vector3f &position, const Quaternion &attitude)
|
|
|
|
{
|
2018-03-25 23:54:05 -03:00
|
|
|
if (!init_sitl_pointer()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-07 21:25:44 -04:00
|
|
|
maybe_send_heartbeat();
|
|
|
|
update_vicon_position_estimate(loc, position, attitude);
|
|
|
|
}
|
|
|
|
|