imu-qmi8658 · PHP API
Qmi8658
Baremetal\I2c\Driver\Qmi8658: the constructor, the three readings with their units, and the 12-byte raw sample that poll() collects and decode() converts.
Baremetal\I2c\Driver\Qmi8658 extends Baremetal\I2c\Device and implements Baremetal\Sensor\Imu. All the
Device methods work on it too; this page covers what the driver adds.
Constructor
new Qmi8658(Bus $bus, int $address = 0x6B, int $hz = 400000)
Public method.
Parameters
-
$busBusrequired The bus the chip is wired to.
-
$addressintoptional, default0x6B 7-bit address. Pass
0x6Afor a chip strapped to its other address.-
$hzintoptional, default400000 SCL clock for this device: 0 uses 400 kHz, otherwise 1 to 1000000 Hz (a negative value throws
\ValueErrorfrom 1.0.2; earlier versions used 400 kHz).
The constructor attaches the device, reads the identity register and configures the chip: address
auto-increment, accelerometer ±4 g, gyroscope ±512 dps, both enabled, then a 10 ms settle. It throws
\Exception:
| Message | Cause |
|---|---|
cannot init qmi8658 at 0x..: ESP_ERR_NOT_FOUND |
Something answered, but its identity is not 0x05. |
cannot init qmi8658 at 0x..: ... |
The identity read or one of the configuration writes failed, for example ESP_ERR_TIMEOUT with no chip. |
address 0x.. on this bus is already used by another driver |
A raw device or another driver holds the address. |
Constructing it again with the same bus and address returns a handle to the same device; the chip is not reconfigured.
Readings
Qmi8658::accel(): array
Public method. Returns array.
[x, y, z] acceleration in g (8192 counts per g). Throws QMI8658 accel read failed.
Qmi8658::gyro(): array
Public method. Returns array.
[x, y, z] angular rate in degrees per second (64 counts per dps). Throws QMI8658 gyro read failed.
Qmi8658::temp(): float
Public method. Returns float.
Chip temperature in °C (raw value / 256). Throws QMI8658 temp read failed.
Each call is one bus transaction (6 bytes for accel() and gyro(), 2 for temp()).
Background samples
Qmi8658::decode(string $raw): array
Public method. Returns array.
Converts one raw sample from sample() or drain() into
['accel' => [x, y, z], 'gyro' => [x, y, z]], in g and degrees per second. Throws \ValueError
(a QMI8658 sample is 12 bytes) for a shorter string.
The driver declares a sampler, so poll(), sample() and drain() work on this class. Each sample is one
12-byte read starting at register 0x35:
| Bytes | Content |
|---|---|
| 0 to 5 | accelerometer x, y, z, signed 16-bit little-endian |
| 6 to 11 | gyroscope x, y, z, signed 16-bit little-endian |
Temperature is not part of the sample. The descriptor states a 1000 Hz maximum rate; ext-i2c does not enforce
it, and poll() cannot go faster than one sample per millisecond anyway.
<?php
use Baremetal\I2c\Bus;
use Baremetal\I2c\Driver\Qmi8658;
$imu = new Qmi8658(new Bus(sda: 11, scl: 10));
$imu->poll(hz: 50, depth: 64);
function loop(): void
{
global $imu;
foreach ($imu->drain() as $raw) {
['accel' => [, , $az]] = $imu->decode($raw);
printf("az % .3f g\n", $az);
}
usleep(250000);
}
Capability
The class implements Baremetal\Sensor\Imu, a marker interface ext-i2c always defines. Type a parameter or an
instanceof check on it to accept any IMU driver rather than this chip; the interface declares no methods.
Related
- Sample in the background Recipe
- Device ext-i2c
- Background sampling and events ext-i2c