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

Crazyflie drivers #9462

Merged
merged 27 commits into from
May 22, 2018
Merged

Crazyflie drivers #9462

merged 27 commits into from
May 22, 2018

Conversation

DanielePettenuzzo
Copy link
Contributor

@DanielePettenuzzo DanielePettenuzzo commented May 14, 2018

This PR enables Crazyflie's optical flow deck by adding the drivers for the optical flow module (pmw3901) and the range sensor (vl53lxx). Moreover adds support for logging on sd card and increases the imu sampling rate.

dpettenuzzo and others added 22 commits May 14, 2018 22:51
@@ -139,7 +139,11 @@ MPU9250::MPU9250(device::Device *interface, device::Device *mag_interface, const
_gyro_range_scale(0.0f),
_gyro_range_rad_s(0.0f),
_dlpf_freq(MPU9250_DEFAULT_ONCHIP_FILTER_FREQ),
#ifdef CONFIG_ARCH_BOARD_CRAZYFLIE
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we find a way to do this without board specific defines in drivers? How about defaulting to 200 Hz for I2C?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dagar I can get it to run up to 330Hz on the crazyflie but at this frequency I get some weird behavior because the delay between two samples becomes quite variable. It seems to work well between 200 and 250Hz. Could changing this create issues on other architectures?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the important difference SPI vs I2C rather than anything crazyflie specific?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I can fix this

MODULE drivers__pmw3901
MAIN pmw3901
COMPILE_FLAGS
-Wno-sign-compare
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed?

SRCS
pmw3901.cpp
DEPENDS
platforms__common
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be deleted, although it's harmless if left.


#include <conversion/rotation.h>

#include <systemlib/perf_counter.h>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<systemlib/perf_counter.h> -> <perf/perf_counter.h>

@DanielePettenuzzo
Copy link
Contributor Author

FYI @barzanisar


usleep(1000);

// set performance optimization registers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielePettenuzzo Any reference we can put here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not really but I can look more

/* For devices competing with NuttX SPI drivers on a bus (Crazyflie SD Card expansion board) */
SPI::set_lockmode(LOCK_THREADS);

/* do I2C init (and probe) first */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment lies

/* notify anyone waiting for data */
poll_notify(POLLIN);

ret = OK;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe just initialize this to 'OK' at the top?

_reports->flush();

/* schedule a cycle to start things */
work_queue(LPWORK, &_work, (worker_t)&PMW3901::cycle_trampoline, this, 1000);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use USEC2TICK(PMW3901_SAMPLE_RATE) here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PMW3901_SAMPLE_RATE is 10ms.. I will create a define for 1ms and use the USEC2TICK

goto fail;
}

exit(0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dagar If we see a chance of this driver ever running in on a linux platform should we then get rid of this NuttX specific stuff?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I would suggest assuming that possibility nearly everywhere as we go and even bother including them in the posix builds (sitl https://github.com/PX4/Firmware/blob/master/cmake/configs/posix_sitl_default.cmake#L6, rpi, etc).

At a minimum it will cause the code to be checked by tools like coverity and clang-tidy.

All that's actually required to "port" most NuttX drivers to Linux/QuRT is to use the px4 headers instead of anything NuttX specific, and prevent it from directly exiting (exit, err).

Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed, the general structure looks good, but needs a second pass to cleanup smaller things.

return -ENODEV;
}

//#ifdef CONFIG_MMCSD
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented?
Same for message("[boot] FAILED to bind SPI port 1 to the MMCSD driver\n");

/* SPI select is active low, so write !selected to select the device */

int sel = (int) devid;
//ASSERT(PX4_SPI_BUS_ID(sel) == PX4_SPI_BUS_EXPANSION);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove if not needed

stm32_configgpio(GPIO_SPI1_MOSI_OFF);

/* set the sensor rail off */
//stm32_gpiowrite(GPIO_VDD_3V3_SENSORS_EN, 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove or uncomment if needed (same below)

@@ -0,0 +1,1223 @@
/****************************************************************************
*
* Copyright (c) 2013-2017 PX4 Development Team. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requires updating

#define DYNAMIC_SPAD_NUM_REQUESTED_REF_SPAD_REG 0x4E
#define GLOBAL_CONFIG_REF_EN_START_SELECT_REG 0xB6
#define GLOBAL_CONFIG_SPAD_ENABLES_REF_0_REG 0xB0
#define SYSTEM_INTERRUPT_CONFIG_GPIO_REG 0x0A
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tabs here are completely off, can you make sure these are aligned?

/* get a publish handle on the range finder topic */
struct distance_sensor_s ds_report;

_reports->get(&ds_report);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are reading from _reports, but nothing has been added to the ringbuffer yet.

switch (ch) {
case 'R':
rotation = (uint8_t)atoi(myoptarg);
PX4_INFO("Setting sensor orientation to %d", (int)rotation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit verbose: can you remove it?

@@ -0,0 +1,944 @@
/****************************************************************************
*
* Copyright (c) 2013-2017 PX4 Development Team. All rights reserved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

requires updating

delete _reports;
}

// if (_class_instance != -1) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

commented code: can you remove it if not needed?

switch (ch) {
case 'R':
rotation = (uint8_t)atoi(myoptarg);
PX4_INFO("Setting sensor orientation to %d", (int)rotation);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit too verbose

* make the integration autoreset faster so that we integrate just one
* sample since the sampling rate is already low.
*/
if (is_i2c()) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bkueng @dagar this is how I reduce the sampling rate and decrease the integration interval when i2c is used for mpu9250. I also had to create the set_autoreset_interval function in the integrator class.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I'd use something like _accel_int.set_autoreset_interval(1000000 / 1000);, so that it does not depend on the macro.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Sounds good!

Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates. I noticed a few more things that require to be fixed.

MODULE drivers__vl53lxx
MAIN vl53lxx
COMPILE_FLAGS
-Wno-sign-compare
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this?

#define VL53LXX_RA_IDENTIFICATION_MODEL_ID 0xC0
#define VL53LXX_IDENTIFICATION_MODEL_ID 0xEEAA

#define VL53LXX_MS 1000 /* 1ms */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is in us, so should be named VL53LXX_US

return ret;
}

ret = OK;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed

work_queue(LPWORK, &_work, (worker_t)&VL53LXX::cycle_trampoline, this, USEC2TICK(VL53LXX_MS));

/* notify about state change */
struct subsystem_info_s info = {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

info.timestamp is not set

info.ok = true;
info.subsystem_type = subsystem_info_s::SUBSYSTEM_TYPE_RANGEFINDER;

static orb_advert_t pub = nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a class member instead of static

info.ok = true;
info.subsystem_type = subsystem_info_s::SUBSYSTEM_TYPE_OPTICALFLOW;

static orb_advert_t pub = nullptr;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid static

}

/* read from the sensor */
ret = transfer(nullptr, 0, &val[0], length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can have a buffer overflow here if readRegisterMulti is called with length > 6. Can you directly use value instead of val?


cmd[0] = reg_address;

memcpy(&cmd[1], &value[0], length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a length check, or an API documentation for writeRegisterMulti about the maximum length (length must not be more than 5).

Copy link
Member

@bkueng bkueng left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to go

@bkueng bkueng merged commit 9cad11d into master May 22, 2018
@bkueng bkueng deleted the pr-crazyflie-drivers branch May 22, 2018 10:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants