33 #include "physical_constants.h"
35 #if defined(PIOS_INCLUDE_PX4FLOW)
43 #define PX4FLOW_TASK_PRIORITY PIOS_THREAD_PRIO_HIGH
44 #define PX4FLOW_TASK_STACK_BYTES 512
45 #define PX4FLOW_SAMPLE_PERIOD_MS 5
46 #define PIOS_PX4FLOW_MAX_DOWNSAMPLE 1
51 enum pios_px4flow_dev_magic {
52 PIOS_PX4FLOW_DEV_MAGIC = 0x1dbef871,
60 struct pios_thread *
task;
62 enum pios_px4flow_dev_magic
magic;
68 static void PIOS_PX4Flow_Task(
void *parameters);
70 static struct px4flow_dev *dev;
75 static struct px4flow_dev * PIOS_PX4Flow_alloc(
void)
77 struct px4flow_dev *px4flow_dev;
79 px4flow_dev = (
struct px4flow_dev *)
PIOS_malloc(
sizeof(*px4flow_dev));
80 if (!px4flow_dev)
return (NULL);
82 px4flow_dev->magic = PIOS_PX4FLOW_DEV_MAGIC;
86 if (px4flow_dev->optical_flow_queue == NULL || px4flow_dev->rangefinder_queue == NULL) {
98 static int32_t PIOS_PX4Flow_Validate(
struct px4flow_dev *dev)
102 if (dev->magic != PIOS_PX4FLOW_DEV_MAGIC)
104 if (dev->i2c_id == 0)
115 dev = (
struct px4flow_dev *) PIOS_PX4Flow_alloc();
120 dev->i2c_id = i2c_id;
123 if (PIOS_PX4Flow_Config(cfg) != 0)
129 dev->task =
PIOS_Thread_Create(PIOS_PX4Flow_Task,
"pios_px4flow", PX4FLOW_TASK_STACK_BYTES, NULL, PX4FLOW_TASK_PRIORITY);
142 if (PIOS_PX4Flow_Validate(dev) != 0)
170 static int32_t PIOS_PX4Flow_ReadData()
172 if (PIOS_PX4Flow_Validate(dev) != 0) {
180 uint8_t addr_read[] = {
187 uint16_t frame_count;
188 int16_t pixel_flow_x_sum_px10;
189 int16_t pixel_flow_y_sum_px10;
190 int16_t flow_comp_x_m1000;
191 int16_t flow_comp_y_m1000;
197 uint8_t sonar_timestamp;
198 int16_t ground_distance_m1000;
213 .len =
sizeof(i2c_frame),
214 .buf = (uint8_t *)&i2c_frame,
225 if (i2c_frame.qual > 255) {
234 if (i2c_frame.sonar_timestamp > 0) {
235 rangefinder_data.
range = i2c_frame.ground_distance_m1000 / 1000.0f;
237 if (rangefinder_data.
range >= 0) {
250 float flow_sensor_frame[3] = {i2c_frame.flow_comp_x_m1000 / 1000.0f, i2c_frame.flow_comp_y_m1000 / 1000.0f, 0 / 1000.0f};
251 float flow_rotated[3];
252 rot_mult(dev->Rsb, flow_sensor_frame, flow_rotated,
true);
253 optical_flow_data.
x_dot = flow_rotated[0];
254 optical_flow_data.
y_dot = flow_rotated[1];
255 optical_flow_data.
z_dot = flow_rotated[2];
257 optical_flow_data.
quality = i2c_frame.qual;
268 static void PIOS_PX4Flow_Task(
void *parameters)
270 while (PIOS_PX4Flow_Validate(dev) != 0) {
279 PIOS_PX4Flow_ReadData();
void rot_mult(float R[3][3], const float vec[3], float vec_out[3], bool transpose)
Rotate a vector by a rotation matrix.
uint32_t PIOS_Thread_Systime(void)
#define PIOS_PX4FLOW_FRAMECOUNTER_LSB
struct pios_queue * PIOS_Queue_Create(size_t queue_length, size_t item_size)
Main PiOS header to include all the compiled in PiOS options.
Pios sensor structure for generic mag data.
void * PIOS_malloc(size_t size)
bool PIOS_Queue_Send(struct pios_queue *queuep, const void *itemp, uint32_t timeout_ms)
static struct flyingpicmd_cfg_fa cfg
Pios sensor structure for generic rangefinder data.
int32_t PIOS_PX4Flow_SetRotation(const struct Rotation rotation)
struct pios_i2c_adapter * pios_i2c_t
struct pios_thread * PIOS_Thread_Create(void(*fp)(void *), const char *namep, size_t stack_bytes, void *argp, enum pios_thread_prio_e prio)
int32_t PIOS_SENSORS_Register(enum pios_sensor_type type, struct pios_queue *queue)
Register a queue-based sensor with the PIOS_SENSORS interface.
static TaskInfoRunningElem task
int32_t PIOS_I2C_Transfer(pios_i2c_t i2c_id, const struct pios_i2c_txn txn_list[], uint32_t num_txns)
void PIOS_Thread_Sleep_Until(uint32_t *previous_ms, uint32_t increment_ms)
void Euler2R(float rpy[3], float Rbe[3][3])
void PIOS_free(void *buf)
void PIOS_Thread_Sleep(uint32_t time_ms)
Header for Coordinate conversions library in coordinate_conversions.c.
int32_t PIOS_PX4Flow_Init(const struct pios_px4flow_cfg *cfg, pios_i2c_t i2c_id)
#define PIOS_PX4FLOW_I2C_7_BIT_ADDR
#define PIOS_Assert(test)