updates to gazebo so simulate pressure and acceleration (#2120)

This commit is contained in:
Kirk Scheper
2017-10-18 22:30:07 +02:00
committed by Gautier Hattenberger
parent 10972bd50f
commit 68255aa454
11 changed files with 86 additions and 37 deletions
+9 -2
View File
@@ -32,7 +32,14 @@ DEBUG_FLAGS ?= -ggdb3
# Launch with "make Q=''" to get full command display # Launch with "make Q=''" to get full command display
Q=@ Q=@
CFLAGS = -W -Wall # flags for warnings (C and C++)
WARN_FLAGS += -W -Wall -Wextra
WARN_FLAGS += -Wunused -Wcast-align -Wpointer-arith -Wswitch-default -Wmissing-declarations
#WARN_FLAGS += -Wcast-qual
#WARN_FLAGS += -Wredundant-decls
#WARN_FLAGS += -Wshadow
CFLAGS = $(WARN_FLAGS)
CFLAGS += $(INCLUDES) CFLAGS += $(INCLUDES)
CFLAGS += $($(TARGET).CFLAGS) CFLAGS += $($(TARGET).CFLAGS)
CFLAGS += $(USER_CFLAGS) $(BOARD_CFLAGS) CFLAGS += $(USER_CFLAGS) $(BOARD_CFLAGS)
@@ -41,7 +48,7 @@ CFLAGS += $(DEBUG_FLAGS)
CFLAGS += -std=gnu99 CFLAGS += -std=gnu99
CFLAGS += $(shell pkg-config --cflags-only-I ivy-glib) CFLAGS += $(shell pkg-config --cflags-only-I ivy-glib)
CXXFLAGS = -W -Wall CXXFLAGS = $(WARN_FLAGS)
CXXFLAGS += $(INCLUDES) CXXFLAGS += $(INCLUDES)
CXXFLAGS += $($(TARGET).CFLAGS) CXXFLAGS += $($(TARGET).CFLAGS)
CXXFLAGS += $($(TARGET).CXXFLAGS) CXXFLAGS += $($(TARGET).CXXFLAGS)
+7 -2
View File
@@ -83,10 +83,15 @@
<header/> <header/>
<makefile target="nps"> <makefile target="nps">
<raw> <raw>
nps.CXXFLAGS += $(shell pkg-config gazebo --cflags) nps.CXXFLAGS += $(shell pkg-config gazebo --cflags opencv)
nps.LDFLAGS += $(shell pkg-config gazebo --libs) nps.LDFLAGS += $(shell pkg-config gazebo --libs)
</raw> </raw>
<file name="nps_fdm_gazebo.cpp" dir="nps"/> <file name="nps_fdm_gazebo.cpp" dir="nps"/>
<!--OPENCV-->
<flag name="LDFLAGS" value="lopencv_imgproc" />
<flag name="LDFLAGS" value="lopencv_highgui" />
<flag name="LDFLAGS" value="lopencv_core" />
</makefile> </makefile>
</module> </module>
+1
View File
@@ -1,3 +1,4 @@
#include "mcu_arch.h"
void mcu_arch_init(void) {} void mcu_arch_init(void) {}
+1 -1
View File
@@ -30,7 +30,7 @@
bool i2c_idle(struct i2c_periph *p __attribute__((unused))) { return true; } bool i2c_idle(struct i2c_periph *p __attribute__((unused))) { return true; }
bool i2c_submit(struct i2c_periph *p __attribute__((unused)), struct i2c_transaction *t __attribute__((unused))) { return true;} bool i2c_submit(struct i2c_periph *p __attribute__((unused)), struct i2c_transaction *t __attribute__((unused))) { return true;}
void i2c_event(void) { } void i2c_event(void) { }
void i2c2_setbitrate(int bitrate __attribute__((unused))) { } void i2c_setbitrate(struct i2c_periph *p __attribute__((unused)), int bitrate __attribute__((unused))) { }
#if USE_I2C0 #if USE_I2C0
struct i2c_errors i2c0_errors; struct i2c_errors i2c0_errors;
@@ -146,11 +146,13 @@ void nps_autopilot_run_step(double time)
} }
#endif #endif
#ifndef NPS_NO_GPS
if (nps_sensors_gps_available()) { if (nps_sensors_gps_available()) {
gps_feed_value(); gps_feed_value();
Fbw(event_task); Fbw(event_task);
Ap(event_task); Ap(event_task);
} }
#endif
#if USE_SONAR #if USE_SONAR
if (nps_sensors_sonar_available()) { if (nps_sensors_sonar_available()) {
+1 -1
View File
@@ -140,7 +140,7 @@ void nps_autopilot_run_step(double time)
} }
#endif #endif
#if USE_GPS #if USE_GPS && !defined(NPS_NO_GPS)
if (nps_sensors_gps_available()) { if (nps_sensors_gps_available()) {
gps_feed_value(); gps_feed_value();
main_event(); main_event();
+53 -23
View File
@@ -31,10 +31,19 @@
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#ifdef NPS_DEBUG_VIDEO
// Opencv tools
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#endif
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <sys/time.h>
#include <gazebo/gazebo.hh> #include <gazebo/gazebo.hh>
#include <gazebo/common/common.hh> #include <gazebo/common/common.hh>
@@ -43,20 +52,13 @@
#include <gazebo/gazebo_config.h> #include <gazebo/gazebo_config.h>
extern "C" { extern "C" {
#include <sys/time.h>
#include "nps_fdm.h" #include "nps_fdm.h"
#include "math/pprz_algebra_double.h"
#include "generated/airframe.h" #include "generated/airframe.h"
#include "autopilot.h" #include "autopilot.h"
#ifdef NPS_SIMULATE_VIDEO #include "math/pprz_isa.h"
#include "modules/computer_vision/cv.h" #include "math/pprz_algebra_double.h"
#include "modules/computer_vision/video_thread_nps.h"
#include "modules/computer_vision/lib/vision/image.h"
#include "mcu_periph/sys_time.h"
#endif
} }
using namespace std; using namespace std;
@@ -70,6 +72,13 @@ using namespace std;
// Add video handling functions if req'd. // Add video handling functions if req'd.
#ifdef NPS_SIMULATE_VIDEO #ifdef NPS_SIMULATE_VIDEO
extern "C" {
#include "modules/computer_vision/cv.h"
#include "modules/computer_vision/video_thread_nps.h"
#include "modules/computer_vision/lib/vision/image.h"
#include "mcu_periph/sys_time.h"
}
static void init_gazebo_video(void); static void init_gazebo_video(void);
static void gazebo_read_video(void); static void gazebo_read_video(void);
static void read_image( static void read_image(
@@ -90,10 +99,13 @@ struct NpsFdm fdm;
static bool gazebo_initialized = false; static bool gazebo_initialized = false;
static gazebo::physics::ModelPtr model = NULL; static gazebo::physics::ModelPtr model = NULL;
// Get contact sensor
static gazebo::sensors::ContactSensorPtr ct;
// Helper functions // Helper functions
static void init_gazebo(void); static void init_gazebo(void);
static void gazebo_read(void); static void gazebo_read(void);
static void gazebo_write(double commands[], int commands_nb); static void gazebo_write(double act_commands[], int commands_nb);
// Conversion routines // Conversion routines
inline struct EcefCoor_d to_pprz_ecef(ignition::math::Vector3d ecef_i) inline struct EcefCoor_d to_pprz_ecef(ignition::math::Vector3d ecef_i)
@@ -175,12 +187,12 @@ void nps_fdm_init(double dt)
/** /**
* Update the simulation state. * Update the simulation state.
* @param launch * @param launch
* @param commands * @param act_commands
* @param commands_nb * @param commands_nb
*/ */
void nps_fdm_run_step( void nps_fdm_run_step(
bool launch __attribute__((unused)), bool launch __attribute__((unused)),
double *commands, double *act_commands,
int commands_nb) int commands_nb)
{ {
// Initialize Gazebo if req'd. // Initialize Gazebo if req'd.
@@ -201,7 +213,7 @@ void nps_fdm_run_step(
// Update the simulation for a single timestep. // Update the simulation for a single timestep.
gazebo::runWorld(model->GetWorld(), 1); gazebo::runWorld(model->GetWorld(), 1);
gazebo::sensors::run_once(); gazebo::sensors::run_once();
gazebo_write(commands, commands_nb); gazebo_write(act_commands, commands_nb);
gazebo_read(); gazebo_read();
#ifdef NPS_SIMULATE_VIDEO #ifdef NPS_SIMULATE_VIDEO
gazebo_read_video(); gazebo_read_video();
@@ -286,6 +298,11 @@ static void init_gazebo(void)
gazebo::runWorld(world, 1); gazebo::runWorld(world, 1);
cout << "Sensors initialized..." << endl; cout << "Sensors initialized..." << endl;
// activate collision sensor
gazebo::sensors::SensorManager *mgr = gazebo::sensors::SensorManager::Instance();
ct = static_pointer_cast < gazebo::sensors::ContactSensor > (mgr->GetSensor("contactsensor"));
ct->SetActive(true);
cout << "Gazebo initialized successfully!" << endl; cout << "Gazebo initialized successfully!" << endl;
} }
@@ -361,9 +378,15 @@ static void gazebo_read(void)
gazebo::common::SphericalCoordinates::GLOBAL)); // Note: unused gazebo::common::SphericalCoordinates::GLOBAL)); // Note: unused
fdm.ltpprz_ecef_accel = fdm.ltp_ecef_accel; // ??? fdm.ltpprz_ecef_accel = fdm.ltp_ecef_accel; // ???
fdm.body_inertial_accel = fdm.body_ecef_accel; // Approximate, unused. fdm.body_inertial_accel = fdm.body_ecef_accel; // Approximate, unused.
fdm.body_accel = to_pprz_body(
pose.rot.RotateVectorReverse(accel.Ign() - world->Gravity()));
// only use accelerometer if no collisions or if not on ground
if (ct->Contacts().contact_size() || pose.pos.z < 0.03){
fdm.body_accel = to_pprz_body(
pose.rot.RotateVectorReverse(-world->Gravity()));
} else {
fdm.body_accel = to_pprz_body(
pose.rot.RotateVectorReverse(accel.Ign() - world->Gravity()));
}
/* attitude */ /* attitude */
// ecef_to_body_quat: unused // ecef_to_body_quat: unused
fdm.ltp_to_body_eulers = to_pprz_eulers(local_to_global_quat * pose.rot); fdm.ltp_to_body_eulers = to_pprz_eulers(local_to_global_quat * pose.rot);
@@ -398,7 +421,7 @@ static void gazebo_read(void)
fdm.pressure_sl = 101325; // Pa fdm.pressure_sl = 101325; // Pa
fdm.airspeed = 0; fdm.airspeed = 0;
fdm.pressure = fdm.pressure_sl; // Pa fdm.pressure = pprz_isa_pressure_of_height(fdm.hmsl, fdm.pressure_sl);
fdm.dynamic_pressure = fdm.pressure_sl; // Pa fdm.dynamic_pressure = fdm.pressure_sl; // Pa
fdm.temperature = 20.0; // C fdm.temperature = 20.0; // C
fdm.aoa = 0; // rad fdm.aoa = 0; // rad
@@ -428,23 +451,21 @@ static void gazebo_read(void)
* NPS_ACTUATOR_NAMES. See conf/simulator/gazebo/models/ardrone/ardrone.sdf * NPS_ACTUATOR_NAMES. See conf/simulator/gazebo/models/ardrone/ardrone.sdf
* for an example. * for an example.
* *
* @param commands * @param act_commands
* @param commands_nb * @param commands_nb
*/ */
static void gazebo_write(double commands[], int commands_nb) static void gazebo_write(double act_commands[], int commands_nb)
{ {
const string names[] = NPS_ACTUATOR_NAMES; const string names[] = NPS_ACTUATOR_NAMES;
const double thrusts[] = { NPS_ACTUATOR_THRUSTS }; const double thrusts[] = { NPS_ACTUATOR_THRUSTS };
const double torques[] = { NPS_ACTUATOR_TORQUES }; const double torques[] = { NPS_ACTUATOR_TORQUES };
for (int i = 0; i < commands_nb; ++i) { for (int i = 0; i < commands_nb; ++i) {
double thrust = autopilot.motors_on ? thrusts[i] * commands[i] : 0.0; double thrust = autopilot.motors_on ? thrusts[i] * act_commands[i] : 0.0;
double torque = autopilot.motors_on ? torques[i] * commands[i] : 0.0; double torque = autopilot.motors_on ? torques[i] * act_commands[i] : 0.0;
gazebo::physics::LinkPtr link = model->GetLink(names[i]); gazebo::physics::LinkPtr link = model->GetLink(names[i]);
link->AddRelativeForce(gazebo::math::Vector3(0, 0, thrust)); link->AddRelativeForce(gazebo::math::Vector3(0, 0, thrust));
link->AddRelativeTorque(gazebo::math::Vector3(0, 0, torque)); link->AddRelativeTorque(gazebo::math::Vector3(0, 0, torque));
// cout << "Motor '" << link->GetName() << "': thrust = " << thrust
// << " N, torque = " << torque << " Nm" << endl;
} }
} }
@@ -526,11 +547,20 @@ static void gazebo_read_video(void)
if (cam == NULL) { continue; } if (cam == NULL) { continue; }
// Skip if not updated // Skip if not updated
// Also skip when LastMeasurementTime() is zero (workaround) // Also skip when LastMeasurementTime() is zero (workaround)
if (cam->LastMeasurementTime() == gazebo_cams[i].last_measurement_time if ((cam->LastMeasurementTime() - gazebo_cams[i].last_measurement_time).Float() < 0.005
|| cam->LastMeasurementTime() == 0) { continue; } || cam->LastMeasurementTime() == 0) { continue; }
// Grab image, convert and send to video thread // Grab image, convert and send to video thread
struct image_t img; struct image_t img;
read_image(&img, cam); read_image(&img, cam);
#ifdef NPS_DEBUG_VIDEO
cv::Mat RGB_cam(cam->ImageHeight(),cam->ImageWidth(),CV_8UC3,(uint8_t *)cam->ImageData());
cv::cvtColor(RGB_cam, RGB_cam, cv::COLOR_RGB2BGR);
cv::namedWindow(cameras[i]->dev_name, cv::WINDOW_AUTOSIZE); // Create a window for display.
cv::imshow(cameras[i]->dev_name, RGB_cam);
cv::waitKey(1);
#endif
cv_run_device(cameras[i], &img); cv_run_device(cameras[i], &img);
// Free image buffer after use. // Free image buffer after use.
image_free(&img); image_free(&img);
+4 -4
View File
@@ -30,7 +30,7 @@ pthread_t th_fg_rx; // fligh gear receive thread
void* nps_flightgear_receive(void* data __attribute__((unused))); void* nps_flightgear_receive(void* data __attribute__((unused)));
double htond(double x) static double htond(double x)
{ {
int *p = (int *)&x; int *p = (int *)&x;
int tmp = p[0]; int tmp = p[0];
@@ -41,7 +41,7 @@ double htond(double x)
} }
float htonf(float x) static float htonf(float x)
{ {
int *p = (int *)&x; int *p = (int *)&x;
*p = htonl(*p); *p = htonl(*p);
@@ -112,7 +112,7 @@ void nps_flightgear_init(const char *host, unsigned int port, unsigned int port
* For visualization with moving surfaces (elevator, propeller etc). * For visualization with moving surfaces (elevator, propeller etc).
* start fgfs with --native-fdm=socket... option * start fgfs with --native-fdm=socket... option
*/ */
void nps_flightgear_send_fdm() void nps_flightgear_send_fdm(void)
{ {
struct FGNetFDM fgfdm; struct FGNetFDM fgfdm;
@@ -176,7 +176,7 @@ void nps_flightgear_send_fdm()
* *
* This is the default option * This is the default option
*/ */
void nps_flightgear_send() void nps_flightgear_send(void)
{ {
struct FGNetGUI gui; struct FGNetGUI gui;
+2 -4
View File
@@ -1,10 +1,8 @@
#ifndef NPS_FLIGHTGEAR_H #ifndef NPS_FLIGHTGEAR_H
#define NPS_FLIGHTGEAR_H #define NPS_FLIGHTGEAR_H
extern void nps_flightgear_init(const char* host, unsigned int port, unsigned int port_in, unsigned int time_offset); extern void nps_flightgear_init(const char* host, unsigned int port, unsigned int port_in, unsigned int time_offset);
extern void nps_flightgear_send(); extern void nps_flightgear_send(void);
extern void nps_flightgear_send_fdm(); extern void nps_flightgear_send_fdm(void);
#endif /* NPS_FLIGHTGEAR_H */ #endif /* NPS_FLIGHTGEAR_H */
+2
View File
@@ -226,6 +226,8 @@ bool nps_main_parse_options(int argc, char **argv)
nps_main.fg_fdm = 1; break; nps_main.fg_fdm = 1; break;
case 10: case 10:
nps_main.fg_port_in = atoi(optarg); break; nps_main.fg_port_in = atoi(optarg); break;
default:
break;
} }
break; break;
@@ -1,3 +1,4 @@
#include "nps_radio_control_spektrum.h"
#include "nps_radio_control.h" #include "nps_radio_control.h"
#include <glib.h> #include <glib.h>
@@ -130,6 +131,9 @@ static void parse_data(char *buf, int len)
handle_frame(); handle_frame();
} }
break; break;
default:
status = STA_UNINIT;
break;
} }
} }
} }