57 SPI(
"LSM303D", nullptr, bus, device,
SPIDEV_MODE3, 11 * 1000 * 1000),
58 ScheduledWorkItem(MODULE_NAME,
px4::device_bus_to_wq(get_device_id())),
92 PX4_ERR(
"SPI init failed (%i)", ret);
166 transfer(cmd, cmd,
sizeof(cmd));
179 transfer(cmd,
nullptr,
sizeof(cmd));
208 float new_scale_g_digit = 0.0f;
217 new_scale_g_digit = 0.061e-3
f;
219 }
else if (max_g <= 4) {
222 new_scale_g_digit = 0.122e-3
f;
224 }
else if (max_g <= 6) {
227 new_scale_g_digit = 0.183e-3
f;
229 }
else if (max_g <= 8) {
232 new_scale_g_digit = 0.244e-3
f;
234 }
else if (max_g <= 16) {
237 new_scale_g_digit = 0.732e-3
f;
257 float new_scale_ga_digit = 0.0f;
266 new_scale_ga_digit = 0.080e-3
f;
268 }
else if (max_ga <= 4) {
271 new_scale_ga_digit = 0.160e-3
f;
273 }
else if (max_ga <= 8) {
276 new_scale_ga_digit = 0.320e-3
f;
278 }
else if (max_ga <= 12) {
281 new_scale_ga_digit = 0.479e-3
f;
300 if (bandwidth == 0) {
304 if (bandwidth <= 50) {
308 }
else if (bandwidth <= 194) {
312 }
else if (bandwidth <= 362) {
316 }
else if (bandwidth <= 773) {
335 if (frequency == 0) {
339 int accel_samplerate = 100;
341 if (frequency <= 100) {
343 accel_samplerate = 100;
345 }
else if (frequency <= 200) {
347 accel_samplerate = 200;
349 }
else if (frequency <= 400) {
351 accel_samplerate = 400;
353 }
else if (frequency <= 800) {
355 accel_samplerate = 800;
357 }
else if (frequency <= 1600) {
359 accel_samplerate = 1600;
379 if (frequency == 0) {
383 int mag_samplerate = 100;
385 if (frequency <= 25) {
389 }
else if (frequency <= 50) {
393 }
else if (frequency <= 100) {
395 mag_samplerate = 100;
455 if (_checked_next != 0) {
471 #pragma pack(push, 1) 478 } raw_accel_report{};
494 transfer((uint8_t *)&raw_accel_report, (uint8_t *)&raw_accel_report,
sizeof(raw_accel_report));
507 if (((
_last_accel[0] - raw_accel_report.x) == 0) &&
537 _px4_accel.
update(timestamp_sample, raw_accel_report.x, raw_accel_report.y, raw_accel_report.z);
552 #pragma pack(push, 1) 566 transfer((uint8_t *)&raw_mag_report, (uint8_t *)&raw_mag_report,
sizeof(raw_mag_report));
577 _px4_mag.
update(timestamp_sample, raw_mag_report.x, raw_mag_report.y, raw_mag_report.z);
599 ::printf(
"reg %02x:%02x should be %02x\n",
void check_registers(void)
check key registers for correct values
uint8_t _checked_values[LSM303D_NUM_CHECKED_REGISTERS]
#define REG2_FULL_SCALE_8G_A
int mag_set_samplerate(unsigned frequency)
Set the LSM303D internal mag sampling frequency.
perf_counter_t _bad_registers
static struct vehicle_status_s status
uint8_t _constant_accel_count
void write_checked_reg(unsigned reg, uint8_t value)
Write a register in the LSM303D, updating _checked_values.
measure the time elapsed performing an event
void set_sample_rate(unsigned rate)
void set_device_type(uint8_t devtype)
#define REG2_FULL_SCALE_4G_A
PX4Accelerometer _px4_accel
void set_error_count(uint64_t error_count)
#define REG2_FULL_SCALE_BITS_A
#define LSM303D_ACCEL_DEFAULT_ONCHIP_FILTER_FREQ
#define REG2_AA_FILTER_BW_773HZ_A
void update(hrt_abstime timestamp, float x, float y, float z)
#define REG2_ANTIALIAS_FILTER_BW_BITS_A
#define REG_STATUS_A_NEW_ZYXADA
#define REG2_AA_FILTER_BW_362HZ_A
#define LSM303D_MAG_DEFAULT_RANGE_GA
void modify_reg(unsigned reg, uint8_t clearbits, uint8_t setbits)
Modify a register in the LSM303D.
uint8_t read_reg(unsigned reg)
Read a register from the LSM303D.
#define LSM303D_ACCEL_DEFAULT_RANGE_G
Namespace encapsulating all device framework classes, functions and data.
count the number of times an event occurs
int accel_set_onchip_lowpass_filter_bandwidth(unsigned bandwidth)
Set the LSM303D on-chip anti-alias filter bandwith.
#define LSM303D_TIMER_REDUCTION
static constexpr float CONSTANTS_ONE_G
void set_error_count(uint64_t error_count)
#define DRV_ACC_DEVTYPE_LSM303D
void set_external(bool external)
#define REG6_FULL_SCALE_12GA_M
int accel_set_samplerate(unsigned frequency)
Set the LSM303D internal accel sampling frequency.
#define DRV_MAG_DEVTYPE_LSM303D
#define REG5_RATE_100HZ_M
void print_info()
Diagnostics - print some basic information about the driver.
#define REG6_FULL_SCALE_4GA_M
perf_counter_t _accel_sample_perf
perf_counter_t _bad_values
void perf_count(perf_counter_t handle)
Count a performance event.
void perf_free(perf_counter_t handle)
Free a counter.
void set_temperature(float temperature)
void init()
Activates/configures the hardware registers.
#define LSM303D_ACCEL_DEFAULT_RATE
#define REG2_FULL_SCALE_2G_A
void set_temperature(float temperature)
#define REG6_FULL_SCALE_8GA_M
void update(hrt_abstime timestamp, int16_t x, int16_t y, int16_t z)
Rotation
Enum for board and external compass rotations.
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
static hrt_abstime hrt_elapsed_time(const hrt_abstime *then)
Compute the delta between a timestamp taken in the past and now.
#define REG2_FULL_SCALE_16G_A
unsigned _call_accel_interval
void write_reg(unsigned reg, uint8_t value)
Write a register in the LSM303D.
#define REG2_AA_FILTER_BW_194HZ_A
void perf_end(perf_counter_t handle)
End a performance event.
unsigned _call_mag_interval
__BEGIN_DECLS typedef uint64_t hrt_abstime
Absolute time, in microsecond units.
perf_counter_t _accel_duplicates
void set_scale(float scale)
void set_scale(float scale)
#define REG6_FULL_SCALE_BITS_M
LSM303D(int bus, uint32_t device, enum Rotation rotation)
#define LSM303D_MAG_DEFAULT_RATE
void perf_print_counter(perf_counter_t handle)
Print one performance counter to stdout.
uint64_t perf_event_count(perf_counter_t handle)
Return current event_count.
static constexpr uint8_t _checked_registers[]
perf_counter_t _mag_sample_perf
#define REG2_AA_FILTER_BW_50HZ_A
void measureAccelerometer()
Fetch accel measurements from the sensor and update the report ring.
#define REG1_RATE_200HZ_A
int accel_set_range(unsigned max_g)
Set the LSM303D accel measurement range.
hrt_abstime _mag_last_measure
Driver for the ST LSM303D MEMS accelerometer / magnetometer connected via SPI.
static constexpr int LSM303D_NUM_CHECKED_REGISTERS
void set_device_type(uint8_t devtype)
#define REG1_RATE_100HZ_A
int mag_set_range(unsigned max_g)
Set the LSM303D mag measurement range.
#define REG6_FULL_SCALE_2GA_M
void disable_i2c()
disable I2C on the chip
void measureMagnetometer()
Fetch mag measurements from the sensor and update the report ring.
#define REG1_RATE_800HZ_A
void perf_begin(perf_counter_t handle)
Begin a performance event.
#define REG1_RATE_1600HZ_A
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
#define REG1_RATE_400HZ_A
#define REG2_FULL_SCALE_6G_A