Code for autonomous ground vehicle, Data Bus, 3rd place winner in 2012 Sparkfun AVC.

Dependencies:   Watchdog mbed Schedule SimpleFilter LSM303DLM PinDetect DebounceIn Servo

Committer:
shimniok
Date:
Wed Jun 20 14:57:48 2012 +0000
Revision:
0:826c6171fc1b
Updated documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shimniok 0:826c6171fc1b 1 // MESSAGE OPTICAL_FLOW PACKING
shimniok 0:826c6171fc1b 2
shimniok 0:826c6171fc1b 3 #define MAVLINK_MSG_ID_OPTICAL_FLOW 100
shimniok 0:826c6171fc1b 4
shimniok 0:826c6171fc1b 5 typedef struct __mavlink_optical_flow_t
shimniok 0:826c6171fc1b 6 {
shimniok 0:826c6171fc1b 7 uint64_t time; ///< Timestamp (UNIX)
shimniok 0:826c6171fc1b 8 uint8_t sensor_id; ///< Sensor ID
shimniok 0:826c6171fc1b 9 int16_t flow_x; ///< Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 10 int16_t flow_y; ///< Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 11 uint8_t quality; ///< Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 12 float ground_distance; ///< Ground distance in meters
shimniok 0:826c6171fc1b 13
shimniok 0:826c6171fc1b 14 } mavlink_optical_flow_t;
shimniok 0:826c6171fc1b 15
shimniok 0:826c6171fc1b 16
shimniok 0:826c6171fc1b 17
shimniok 0:826c6171fc1b 18 /**
shimniok 0:826c6171fc1b 19 * @brief Pack a optical_flow message
shimniok 0:826c6171fc1b 20 * @param system_id ID of this system
shimniok 0:826c6171fc1b 21 * @param component_id ID of this component (e.g. 200 for IMU)
shimniok 0:826c6171fc1b 22 * @param msg The MAVLink message to compress the data into
shimniok 0:826c6171fc1b 23 *
shimniok 0:826c6171fc1b 24 * @param time Timestamp (UNIX)
shimniok 0:826c6171fc1b 25 * @param sensor_id Sensor ID
shimniok 0:826c6171fc1b 26 * @param flow_x Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 27 * @param flow_y Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 28 * @param quality Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 29 * @param ground_distance Ground distance in meters
shimniok 0:826c6171fc1b 30 * @return length of the message in bytes (excluding serial stream start sign)
shimniok 0:826c6171fc1b 31 */
shimniok 0:826c6171fc1b 32 static inline uint16_t mavlink_msg_optical_flow_pack(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, uint64_t time, uint8_t sensor_id, int16_t flow_x, int16_t flow_y, uint8_t quality, float ground_distance)
shimniok 0:826c6171fc1b 33 {
shimniok 0:826c6171fc1b 34 uint16_t i = 0;
shimniok 0:826c6171fc1b 35 msg->msgid = MAVLINK_MSG_ID_OPTICAL_FLOW;
shimniok 0:826c6171fc1b 36
shimniok 0:826c6171fc1b 37 i += put_uint64_t_by_index(time, i, msg->payload); // Timestamp (UNIX)
shimniok 0:826c6171fc1b 38 i += put_uint8_t_by_index(sensor_id, i, msg->payload); // Sensor ID
shimniok 0:826c6171fc1b 39 i += put_int16_t_by_index(flow_x, i, msg->payload); // Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 40 i += put_int16_t_by_index(flow_y, i, msg->payload); // Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 41 i += put_uint8_t_by_index(quality, i, msg->payload); // Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 42 i += put_float_by_index(ground_distance, i, msg->payload); // Ground distance in meters
shimniok 0:826c6171fc1b 43
shimniok 0:826c6171fc1b 44 return mavlink_finalize_message(msg, system_id, component_id, i);
shimniok 0:826c6171fc1b 45 }
shimniok 0:826c6171fc1b 46
shimniok 0:826c6171fc1b 47 /**
shimniok 0:826c6171fc1b 48 * @brief Pack a optical_flow message
shimniok 0:826c6171fc1b 49 * @param system_id ID of this system
shimniok 0:826c6171fc1b 50 * @param component_id ID of this component (e.g. 200 for IMU)
shimniok 0:826c6171fc1b 51 * @param chan The MAVLink channel this message was sent over
shimniok 0:826c6171fc1b 52 * @param msg The MAVLink message to compress the data into
shimniok 0:826c6171fc1b 53 * @param time Timestamp (UNIX)
shimniok 0:826c6171fc1b 54 * @param sensor_id Sensor ID
shimniok 0:826c6171fc1b 55 * @param flow_x Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 56 * @param flow_y Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 57 * @param quality Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 58 * @param ground_distance Ground distance in meters
shimniok 0:826c6171fc1b 59 * @return length of the message in bytes (excluding serial stream start sign)
shimniok 0:826c6171fc1b 60 */
shimniok 0:826c6171fc1b 61 static inline uint16_t mavlink_msg_optical_flow_pack_chan(uint8_t system_id, uint8_t component_id, uint8_t chan, mavlink_message_t* msg, uint64_t time, uint8_t sensor_id, int16_t flow_x, int16_t flow_y, uint8_t quality, float ground_distance)
shimniok 0:826c6171fc1b 62 {
shimniok 0:826c6171fc1b 63 uint16_t i = 0;
shimniok 0:826c6171fc1b 64 msg->msgid = MAVLINK_MSG_ID_OPTICAL_FLOW;
shimniok 0:826c6171fc1b 65
shimniok 0:826c6171fc1b 66 i += put_uint64_t_by_index(time, i, msg->payload); // Timestamp (UNIX)
shimniok 0:826c6171fc1b 67 i += put_uint8_t_by_index(sensor_id, i, msg->payload); // Sensor ID
shimniok 0:826c6171fc1b 68 i += put_int16_t_by_index(flow_x, i, msg->payload); // Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 69 i += put_int16_t_by_index(flow_y, i, msg->payload); // Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 70 i += put_uint8_t_by_index(quality, i, msg->payload); // Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 71 i += put_float_by_index(ground_distance, i, msg->payload); // Ground distance in meters
shimniok 0:826c6171fc1b 72
shimniok 0:826c6171fc1b 73 return mavlink_finalize_message_chan(msg, system_id, component_id, chan, i);
shimniok 0:826c6171fc1b 74 }
shimniok 0:826c6171fc1b 75
shimniok 0:826c6171fc1b 76 /**
shimniok 0:826c6171fc1b 77 * @brief Encode a optical_flow struct into a message
shimniok 0:826c6171fc1b 78 *
shimniok 0:826c6171fc1b 79 * @param system_id ID of this system
shimniok 0:826c6171fc1b 80 * @param component_id ID of this component (e.g. 200 for IMU)
shimniok 0:826c6171fc1b 81 * @param msg The MAVLink message to compress the data into
shimniok 0:826c6171fc1b 82 * @param optical_flow C-struct to read the message contents from
shimniok 0:826c6171fc1b 83 */
shimniok 0:826c6171fc1b 84 static inline uint16_t mavlink_msg_optical_flow_encode(uint8_t system_id, uint8_t component_id, mavlink_message_t* msg, const mavlink_optical_flow_t* optical_flow)
shimniok 0:826c6171fc1b 85 {
shimniok 0:826c6171fc1b 86 return mavlink_msg_optical_flow_pack(system_id, component_id, msg, optical_flow->time, optical_flow->sensor_id, optical_flow->flow_x, optical_flow->flow_y, optical_flow->quality, optical_flow->ground_distance);
shimniok 0:826c6171fc1b 87 }
shimniok 0:826c6171fc1b 88
shimniok 0:826c6171fc1b 89 /**
shimniok 0:826c6171fc1b 90 * @brief Send a optical_flow message
shimniok 0:826c6171fc1b 91 * @param chan MAVLink channel to send the message
shimniok 0:826c6171fc1b 92 *
shimniok 0:826c6171fc1b 93 * @param time Timestamp (UNIX)
shimniok 0:826c6171fc1b 94 * @param sensor_id Sensor ID
shimniok 0:826c6171fc1b 95 * @param flow_x Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 96 * @param flow_y Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 97 * @param quality Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 98 * @param ground_distance Ground distance in meters
shimniok 0:826c6171fc1b 99 */
shimniok 0:826c6171fc1b 100 #ifdef MAVLINK_USE_CONVENIENCE_FUNCTIONS
shimniok 0:826c6171fc1b 101
shimniok 0:826c6171fc1b 102 static inline void mavlink_msg_optical_flow_send(mavlink_channel_t chan, uint64_t time, uint8_t sensor_id, int16_t flow_x, int16_t flow_y, uint8_t quality, float ground_distance)
shimniok 0:826c6171fc1b 103 {
shimniok 0:826c6171fc1b 104 mavlink_message_t msg;
shimniok 0:826c6171fc1b 105 mavlink_msg_optical_flow_pack_chan(mavlink_system.sysid, mavlink_system.compid, chan, &msg, time, sensor_id, flow_x, flow_y, quality, ground_distance);
shimniok 0:826c6171fc1b 106 mavlink_send_uart(chan, &msg);
shimniok 0:826c6171fc1b 107 }
shimniok 0:826c6171fc1b 108
shimniok 0:826c6171fc1b 109 #endif
shimniok 0:826c6171fc1b 110 // MESSAGE OPTICAL_FLOW UNPACKING
shimniok 0:826c6171fc1b 111
shimniok 0:826c6171fc1b 112 /**
shimniok 0:826c6171fc1b 113 * @brief Get field time from optical_flow message
shimniok 0:826c6171fc1b 114 *
shimniok 0:826c6171fc1b 115 * @return Timestamp (UNIX)
shimniok 0:826c6171fc1b 116 */
shimniok 0:826c6171fc1b 117 static inline uint64_t mavlink_msg_optical_flow_get_time(const mavlink_message_t* msg)
shimniok 0:826c6171fc1b 118 {
shimniok 0:826c6171fc1b 119 generic_64bit r;
shimniok 0:826c6171fc1b 120 r.b[7] = (msg->payload)[0];
shimniok 0:826c6171fc1b 121 r.b[6] = (msg->payload)[1];
shimniok 0:826c6171fc1b 122 r.b[5] = (msg->payload)[2];
shimniok 0:826c6171fc1b 123 r.b[4] = (msg->payload)[3];
shimniok 0:826c6171fc1b 124 r.b[3] = (msg->payload)[4];
shimniok 0:826c6171fc1b 125 r.b[2] = (msg->payload)[5];
shimniok 0:826c6171fc1b 126 r.b[1] = (msg->payload)[6];
shimniok 0:826c6171fc1b 127 r.b[0] = (msg->payload)[7];
shimniok 0:826c6171fc1b 128 return (uint64_t)r.ll;
shimniok 0:826c6171fc1b 129 }
shimniok 0:826c6171fc1b 130
shimniok 0:826c6171fc1b 131 /**
shimniok 0:826c6171fc1b 132 * @brief Get field sensor_id from optical_flow message
shimniok 0:826c6171fc1b 133 *
shimniok 0:826c6171fc1b 134 * @return Sensor ID
shimniok 0:826c6171fc1b 135 */
shimniok 0:826c6171fc1b 136 static inline uint8_t mavlink_msg_optical_flow_get_sensor_id(const mavlink_message_t* msg)
shimniok 0:826c6171fc1b 137 {
shimniok 0:826c6171fc1b 138 return (uint8_t)(msg->payload+sizeof(uint64_t))[0];
shimniok 0:826c6171fc1b 139 }
shimniok 0:826c6171fc1b 140
shimniok 0:826c6171fc1b 141 /**
shimniok 0:826c6171fc1b 142 * @brief Get field flow_x from optical_flow message
shimniok 0:826c6171fc1b 143 *
shimniok 0:826c6171fc1b 144 * @return Flow in pixels in x-sensor direction
shimniok 0:826c6171fc1b 145 */
shimniok 0:826c6171fc1b 146 static inline int16_t mavlink_msg_optical_flow_get_flow_x(const mavlink_message_t* msg)
shimniok 0:826c6171fc1b 147 {
shimniok 0:826c6171fc1b 148 generic_16bit r;
shimniok 0:826c6171fc1b 149 r.b[1] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t))[0];
shimniok 0:826c6171fc1b 150 r.b[0] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t))[1];
shimniok 0:826c6171fc1b 151 return (int16_t)r.s;
shimniok 0:826c6171fc1b 152 }
shimniok 0:826c6171fc1b 153
shimniok 0:826c6171fc1b 154 /**
shimniok 0:826c6171fc1b 155 * @brief Get field flow_y from optical_flow message
shimniok 0:826c6171fc1b 156 *
shimniok 0:826c6171fc1b 157 * @return Flow in pixels in y-sensor direction
shimniok 0:826c6171fc1b 158 */
shimniok 0:826c6171fc1b 159 static inline int16_t mavlink_msg_optical_flow_get_flow_y(const mavlink_message_t* msg)
shimniok 0:826c6171fc1b 160 {
shimniok 0:826c6171fc1b 161 generic_16bit r;
shimniok 0:826c6171fc1b 162 r.b[1] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t))[0];
shimniok 0:826c6171fc1b 163 r.b[0] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t))[1];
shimniok 0:826c6171fc1b 164 return (int16_t)r.s;
shimniok 0:826c6171fc1b 165 }
shimniok 0:826c6171fc1b 166
shimniok 0:826c6171fc1b 167 /**
shimniok 0:826c6171fc1b 168 * @brief Get field quality from optical_flow message
shimniok 0:826c6171fc1b 169 *
shimniok 0:826c6171fc1b 170 * @return Optical flow quality / confidence. 0: bad, 255: maximum quality
shimniok 0:826c6171fc1b 171 */
shimniok 0:826c6171fc1b 172 static inline uint8_t mavlink_msg_optical_flow_get_quality(const mavlink_message_t* msg)
shimniok 0:826c6171fc1b 173 {
shimniok 0:826c6171fc1b 174 return (uint8_t)(msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t)+sizeof(int16_t))[0];
shimniok 0:826c6171fc1b 175 }
shimniok 0:826c6171fc1b 176
shimniok 0:826c6171fc1b 177 /**
shimniok 0:826c6171fc1b 178 * @brief Get field ground_distance from optical_flow message
shimniok 0:826c6171fc1b 179 *
shimniok 0:826c6171fc1b 180 * @return Ground distance in meters
shimniok 0:826c6171fc1b 181 */
shimniok 0:826c6171fc1b 182 static inline float mavlink_msg_optical_flow_get_ground_distance(const mavlink_message_t* msg)
shimniok 0:826c6171fc1b 183 {
shimniok 0:826c6171fc1b 184 generic_32bit r;
shimniok 0:826c6171fc1b 185 r.b[3] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t)+sizeof(int16_t)+sizeof(uint8_t))[0];
shimniok 0:826c6171fc1b 186 r.b[2] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t)+sizeof(int16_t)+sizeof(uint8_t))[1];
shimniok 0:826c6171fc1b 187 r.b[1] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t)+sizeof(int16_t)+sizeof(uint8_t))[2];
shimniok 0:826c6171fc1b 188 r.b[0] = (msg->payload+sizeof(uint64_t)+sizeof(uint8_t)+sizeof(int16_t)+sizeof(int16_t)+sizeof(uint8_t))[3];
shimniok 0:826c6171fc1b 189 return (float)r.f;
shimniok 0:826c6171fc1b 190 }
shimniok 0:826c6171fc1b 191
shimniok 0:826c6171fc1b 192 /**
shimniok 0:826c6171fc1b 193 * @brief Decode a optical_flow message into a struct
shimniok 0:826c6171fc1b 194 *
shimniok 0:826c6171fc1b 195 * @param msg The message to decode
shimniok 0:826c6171fc1b 196 * @param optical_flow C-struct to decode the message contents into
shimniok 0:826c6171fc1b 197 */
shimniok 0:826c6171fc1b 198 static inline void mavlink_msg_optical_flow_decode(const mavlink_message_t* msg, mavlink_optical_flow_t* optical_flow)
shimniok 0:826c6171fc1b 199 {
shimniok 0:826c6171fc1b 200 optical_flow->time = mavlink_msg_optical_flow_get_time(msg);
shimniok 0:826c6171fc1b 201 optical_flow->sensor_id = mavlink_msg_optical_flow_get_sensor_id(msg);
shimniok 0:826c6171fc1b 202 optical_flow->flow_x = mavlink_msg_optical_flow_get_flow_x(msg);
shimniok 0:826c6171fc1b 203 optical_flow->flow_y = mavlink_msg_optical_flow_get_flow_y(msg);
shimniok 0:826c6171fc1b 204 optical_flow->quality = mavlink_msg_optical_flow_get_quality(msg);
shimniok 0:826c6171fc1b 205 optical_flow->ground_distance = mavlink_msg_optical_flow_get_ground_distance(msg);
shimniok 0:826c6171fc1b 206 }