39 #include <px4_platform_common/tasks.h> 41 #include <px4_platform_common/log.h> 42 #include <px4_platform_common/getopt.h> 61 #include <semaphore.h> 70 #define MAX_LEN_DEV_PATH 32 73 #define SPI_INT_GPIO 65 // GPIO pin for Data Ready Interrupt 90 static struct mpu9x50_data
_data;
105 static enum gyro_lpf_e
_gyro_lpf = MPU9X50_GYRO_LPF_20HZ;
146 static void task_main(
int argc,
char *argv[]);
180 if (exit_mreasurement) {
186 px4_task_kill(_task_handle, SIGRTMIN);
207 PX4_DEBUG(
"mpu9x50_parameters_update");
214 PX4_DEBUG(
"mpu9x50_parameters_update accel_x_offset %f", v);
219 PX4_DEBUG(
"mpu9x50_parameters_update accel_x_scale %f", v);
224 PX4_DEBUG(
"mpu9x50_parameters_update accel_y_offset %f", v);
229 PX4_DEBUG(
"mpu9x50_parameters_update accel_y_scale %f", v);
234 PX4_DEBUG(
"mpu9x50_parameters_update accel_z_offset %f", v);
239 PX4_DEBUG(
"mpu9x50_parameters_update accel_z_scale %f", v);
245 PX4_DEBUG(
"mpu9x50_parameters_update gyro_x_offset %f", v);
250 PX4_DEBUG(
"mpu9x50_parameters_update gyro_x_scale %f", v);
255 PX4_DEBUG(
"mpu9x50_parameters_update gyro_y_offset %f", v);
260 PX4_DEBUG(
"mpu9x50_parameters_update gyro_y_scale %f", v);
265 PX4_DEBUG(
"mpu9x50_parameters_update gyro_z_offset %f", v);
270 PX4_DEBUG(
"mpu9x50_parameters_update gyro_z_scale %f", v);
276 PX4_DEBUG(
"mpu9x50_parameters_update mag_x_offset %f", v);
281 PX4_DEBUG(
"mpu9x50_parameters_update mag_x_scale %f", v);
286 PX4_DEBUG(
"mpu9x50_parameters_update mag_y_offset %f", v);
291 PX4_DEBUG(
"mpu9x50_parameters_update mag_y_scale %f", v);
296 PX4_DEBUG(
"mpu9x50_parameters_update mag_z_offset %f", v);
301 PX4_DEBUG(
"mpu9x50_parameters_update mag_z_scale %f", v);
306 if (v_int >= NUM_MPU9X50_GYRO_LPF) {
307 PX4_WARN(
"invalid gyro_lpf_enum %d use default %d", v_int, _gyro_lpf);
310 _gyro_lpf = (
enum gyro_lpf_e)v_int;
311 PX4_DEBUG(
"mpu9x50_parameters_update gyro_lpf_enum %d", _gyro_lpf);
316 if (v_int >= NUM_MPU9X50_ACC_LPF) {
317 PX4_WARN(
"invalid accel_lpf_enum %d use default %d", v_int, _accel_lpf);
320 _accel_lpf = (
enum acc_lpf_e)v_int;
321 PX4_DEBUG(
"mpu9x50_parameters_update accel_lpf_enum %d", _accel_lpf);
326 if (v_int >= NUM_MPU9X50_SAMPLE_RATE) {
327 PX4_WARN(
"invalid imu_sample_rate %d use default %d", v_int, _imu_sample_rate);
330 _imu_sample_rate = (
enum gyro_sample_rate_e)v_int;
331 PX4_DEBUG(
"mpu9x50_parameters_update imu_sample_rate %d", _imu_sample_rate);
377 if (_gyro_pub ==
nullptr) {
378 PX4_ERR(
"sensor_gyro advert fail");
385 if (_accel_pub ==
nullptr) {
386 PX4_ERR(
"sensor_accel advert fail");
393 if (_mag_pub ==
nullptr) {
394 PX4_ERR(
"sensor_mag advert fail");
423 if (
_data.mag_data_ready) {
439 PX4_WARN(
"failed to publish gyro report");
447 PX4_WARN(
"failed to publish accel report");
454 if (
_data.mag_data_ready) {
456 PX4_WARN(
"failed to publish mag report");
467 PX4_WARN(
"enter mpu9x50 task_main");
472 exit_mreasurement =
false;
477 struct mpu9x50_config config = {
480 .gyro_fsr = MPU9X50_GYRO_FSR_500DPS,
481 .acc_fsr = MPU9X50_ACC_FSR_4G,
483 .compass_enabled =
true,
484 .compass_sample_rate = MPU9x50_COMPASS_SAMPLE_RATE_100HZ,
493 if (mpu9x50_initialize(&config) != 0) {
494 PX4_WARN(
"error initializing mpu9x50. Quit!");
502 PX4_WARN(
"error registering data ready interrupt. Quit!");
516 sigaddset(&
set, SIGRTMIN);
518 while (!_task_should_exit) {
520 rv = sigwait(&
set, &sig);
523 if (rv != 0 || sig != SIGRTMIN) {
524 PX4_WARN(
"mpu9x50 sigwait failed rv %d sig %d", rv, sig);
529 if (mpu9x50_get_data(&
_data) != 0) {
530 PX4_WARN(
"mpu9x50_get_data() failed");
551 exit_mreasurement =
true;
554 PX4_WARN(
"closing mpu9x50");
561 PX4_WARN(
"task_main_trampoline");
567 ASSERT(_task_handle == -1);
570 _task_handle = px4_task_spawn_cmd(
"mpu9x50_main",
577 if (_task_handle < 0) {
578 warn(
"mpu9x50_main task start failed");
595 PX4_WARN(
"missing command: try 'start', 'stop', 'status'");
596 PX4_WARN(
"options:");
597 PX4_WARN(
" -D device");
605 const char *
device = NULL;
608 const char *myoptarg = NULL;
610 while ((ch = px4_getopt(argc, argv,
"D:", &myoptind, &myoptarg)) != EOF) {
623 if (device == NULL || strlen(device) == 0) {
631 const char *verb = argv[myoptind];
636 if (!strcmp(verb,
"start")) {
638 PX4_WARN(
"mpu9x50 already running");
644 }
else if (!strcmp(verb,
"stop")) {
646 PX4_WARN(
"mpu9x50 is not running");
652 }
else if (!strcmp(verb,
"status")) {
Accelerometer driver interface.
int orb_copy(const struct orb_metadata *meta, int handle, void *buffer)
Gyroscope driver interface.
static char _device[MAX_LEN_DEV_PATH]
SPI device path that mpu9x50 is connected to.
static int _params_sub
parameter update subscription
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
API for the uORB lightweight object broker.
gyro scaling factors; Vout = (Vin * Vscale) + Voffset
static void parameter_update_poll()
poll parameter update
static int _mag_orb_class_instance
instance handle for mag devices
mag scaling factors; Vout = (Vin * Vscale) + Voffset
static int _gyro_orb_class_instance
instance handle for gyro devices
Namespace encapsulating all device framework classes, functions and data.
static orb_advert_t _gyro_pub
gyro data publication
High-resolution timer with callouts and timekeeping.
static bool _task_should_exit
flag indicating if measurement thread should exit
static int _accel_orb_class_instance
instance handle for accel devices
accel scaling factors; Vout = Vscale * (Vin + Voffset)
void start()
Attempt to start driver on all available I2C busses.
static enum gyro_sample_rate_e _imu_sample_rate
IMU sample rate enum.
int orb_subscribe(const struct orb_metadata *meta)
static void stop()
mpu9x50 stop measurement
static void task_main(int argc, char *argv[])
mpu9x50 measurement thread primary entry point
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
static bool create_pubs()
create and advertise all publicatoins
static void parameters_update()
update all parameters
static struct gyro_calibration_s _gyro_sc
gyro scale
static sensor_accel_s _accel
accel report
static void task_main_trampoline(int argc, char *argv[])
task main trampoline function
static struct mag_report _mag
mag report
static void * data_ready_isr(void *context)
MPU9x50 data ready interrupt service routine.
static sensor_gyro_s _gyro
gyro report
static enum gyro_lpf_e _gyro_lpf
gyro lpf enum value
static int64_t _isr_data_ready_timestamp
static void parameters_init()
initialize all parameter handles and load the initial parameter values
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
int orb_publish(const struct orb_metadata *meta, orb_advert_t handle, const void *data)
static void usage()
Print out the usage information.
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
static bool _is_running
flag indicating if mpu9x50 app is running
int orb_check(int handle, bool *updated)
static void update_reports()
update all sensor reports with the latest sensor reading
static struct mag_calibration_s _mag_sc
mag scale
param_t imu_sample_rate_enum
struct mpu9x50::@16 _params_handles
parameter handles
static px4_task_t _task_handle
handle to the task main thread
static orb_advert_t _mag_pub
compass data publication
static struct mpu9x50_data _data
IMU measurement data.
__EXPORT int mpu9x50_main(int argc, char *argv[])
driver 'main' command
orb_advert_t orb_advertise_multi(const struct orb_metadata *meta, const void *data, int *instance, int priority)
static enum acc_lpf_e _accel_lpf
accel lpf enum value
static orb_advert_t _accel_pub
accelerameter data publication
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.
static struct accel_calibration_s _accel_sc
accel scale
static void publish_reports()
publish all sensor reports