sensor_algo.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*******************************************************************************
  2. * @file sensor_algo.h
  3. * @author MEMS Application Team
  4. * @version V1.0
  5. * @date 2021-5-25
  6. * @brief sensor algorithm api
  7. *******************************************************************************/
  8. #ifndef _SENSOR_ALGO_H
  9. #define _SENSOR_ALGO_H
  10. /******************************************************************************/
  11. //includes
  12. /******************************************************************************/
  13. #include <stdint.h>
  14. #include <stdarg.h>
  15. #include <sys/printk.h>
  16. #include <sensor_hal.h>
  17. #include <sensor_algo_common.h>
  18. /******************************************************************************/
  19. //constants
  20. /******************************************************************************/
  21. /* Input sensor id */
  22. typedef enum {
  23. IN_ACC = 0,
  24. IN_GYRO = 1,
  25. IN_MAG = 2,
  26. IN_BARO = 3,
  27. IN_TEMP = 4,
  28. IN_HEARTRATE = 5,
  29. IN_GNSS = 6,
  30. IN_OFFBODY = 7,
  31. IN_ON_CHARGING = 8,
  32. IN_ACC_ANY_MOTION = 9,
  33. IN_SENS1 = 10,
  34. } sensor_in_e;
  35. /* Output sensor id */
  36. typedef enum {
  37. RAW_ACCEL = 0,
  38. RAW_GYRO = 1,
  39. RAW_MAG = 2,
  40. RAW_BARO = 3,
  41. RAW_TEMP = 4,
  42. RAW_HEARTRATE = 5,
  43. RAW_GNSS = 6,
  44. RAW_OFFBODY = 7,
  45. ALGO_SEDENTARY = 8,
  46. ALGO_HANDUP = 9,
  47. ALGO_SLEEP = 10,
  48. REQ_SENSOR = 11,
  49. ALGO_ACTTYPEDETECT = 12,
  50. ALGO_SHAKE = 13,
  51. ALGO_ACTIVITY_OUTPUT = 14,
  52. ALGO_ANY_MOTION = 15,
  53. ALGO_NO_MOTION = 16,
  54. ALGO_FALLING = 17,
  55. ONCHARGING_DETECT = 18,
  56. ALGO_ABSOLUTE_STATIC = 19,
  57. ALGO_SPV = 20,
  58. ALGO_SENS_CALIBRATION = 21,
  59. ALGO_INACTIVITY_OUTPUT = 22,
  60. ALGO_STAND = 23,
  61. ALGO_SENS_RANGE_DETECT = 24,
  62. } sensor_out_e;
  63. /* Activity mode */
  64. typedef enum {
  65. ACTIVITY_NORMAL = 1001, // acc + baro
  66. ACTIVITY_TREADMILL = 1002, // acc + hr
  67. ACTIVITY_OUTDOOR_RUNNING = 1003, // acc + hr + gnss
  68. ACTIVITY_CLIMBING_STAIRS = 1004,
  69. ACTIVITY_HIKING = 1005,
  70. ACTIVITY_INDOOR_RUNNING = 1006,
  71. ACTIVITY_OUTDOOR_RUNNING_TRACKING = 1009,
  72. ACTIVITY_INDOOR_SWIMMING = 2001,
  73. ACTIVITY_OPEN_WATER_SWIMMING = 2002,
  74. ACTIVITY_OUTDOOR_BIKING = 3001, // acc + baro + hr +gnss
  75. ACTIVITY_FREE_TRAINING = 5001,
  76. ACTIVITY_WORKOUT_MACHINE = 6001,
  77. MAIN_EXTRA = 100, // main extra info
  78. AR_ALERT = 102,
  79. SWIM_POOL_LAP_INFO = 110,
  80. SWIM_POOL_TOTAL_INFO = 111,
  81. SWIM_OPEN_WATER_SEGMENT_INFO = 114,
  82. SWIM_OPEN_WATER_TOTAL_INFO = 115,
  83. BIKING_EXTRA = 120, // biking extra info
  84. } activity_mode_e;
  85. typedef enum {
  86. ACTIVITY_OUT_SEDENTARY = 20,
  87. ACTIVITY_OUT_SLEEPING = 21,
  88. ACTIVITY_OUT_NAP = 22,
  89. ACTIVITY_OUT_SLEEPING_EXT = 31,
  90. ACTIVITY_OUT_NAP_EXT = 32,
  91. } inactivity_output_mode_e;
  92. /* Workout Machine*/
  93. typedef enum {
  94. WM_Rope_Skipping,
  95. WM_DoubleUnder_Skipping,
  96. WM_Elliptical,
  97. WM_Rowing_Machine,
  98. } workout_machine_e;
  99. /* Control id */
  100. typedef enum {
  101. SCL_LOG = 0,
  102. SCL_USER_INFO = 1,
  103. SCL_DATE_TIME = 2,
  104. SCL_SEDENTARY = 3,
  105. SCL_PEDO_RESET = 4,
  106. SCL_REQ_SLEEPING_DATA = 5,
  107. SCL_LOW_POWER_MODE = 6,
  108. SCL_SET_ACTIVITY_MODE = 7,
  109. SCL_GET_CHIP_INFO = 8,
  110. SCL_ACTIVITY_CONFIG = 9,
  111. SCL_LIB_DEBUG = 10,
  112. SCL_PEDO_CONFIG = 11,
  113. SCL_GET_LIB_INFO = 12,
  114. SCL_SWIM_CONFIG = 13,
  115. SCL_BIKING_CONFIG = 14,
  116. SCL_ACTIVITY_PAUSE = 15,
  117. SCL_HAND_UPDOWN_CONFIG = 16,
  118. SCL_CHIP_VENDOR_CONFIG = 17,
  119. SCL_SLEEP_CONFIG = 18,
  120. SCL_ABS_STATIC_CONFIG = 19,
  121. SCL_HEART_RATE_CONFIG = 20,
  122. SCL_REQ_SWIM_EXIT = 21,
  123. SCL_WATCH_FALL_CONFIG = 22,
  124. SCL_AR_ALERT_CONFIG = 23,
  125. SCL_ACT_PAUSE_DETECT = 24,
  126. SCL_WM_CONFIG = 25,
  127. SCL_INACTIVITY_CONFIG = 26,
  128. SCL_SET_INACTIVITY_MODE = 27,
  129. SCL_STAND_CONFIG = 28,
  130. SCL_SS_CONFIG = 29,
  131. SCL_ACT_INFO_CONFIG = 30,
  132. SCL_REQ_ACTIVITY_EXIT = 31,
  133. SCL_ACT_PAI_INFO = 33,
  134. SCL_ACT_INTSY_INFO = 35,
  135. SCL_SZ_BREACH_CONFIG = 50,
  136. SCL_WASH_HAND_CONFIG = 52,
  137. SCL_ALGO_PROC_CONFIG = 100,
  138. SCL_INPUT_SENSOR_CONFIG = 101,
  139. SCL_INPUT_DT_CONFIG = 102,
  140. SCL_SENS_RANGE_DETECT_CONFIG = 103,
  141. SCL_SENS_CALI_CONFIG = 110,
  142. SCL_SENS_CALI_CTRL_MAG = 111,
  143. SCL_SENS_CALI_CTRL_A = 112,
  144. SCL_SPV_CONFIG = 113,
  145. SCL_SPV_MODE = 114,
  146. SCL_SENS_CALI_SET_MAG = 115,
  147. SCL_SENS_CALI_SET_A = 116,
  148. } sensor_ctl_e;
  149. /******************************************************************************/
  150. //typedef
  151. /******************************************************************************/
  152. #ifdef __CC_ARM /* ARM Compiler */
  153. #pragma anon_unions
  154. #endif
  155. /* Input sensor data */
  156. typedef struct {
  157. uint32_t id;
  158. float fData[16];
  159. double dData[10];
  160. } sensor_raw_t;
  161. /* Ouput sensor event */
  162. typedef struct {
  163. uint32_t id;
  164. uint32_t index;
  165. float fData[16];
  166. uint64_t timestamp_ns;
  167. void *memData;
  168. } sensor_evt_t;
  169. /* Sensor control data */
  170. typedef struct {
  171. int iData[16];
  172. } sensor_ctl_t;
  173. /* Sensor date time */
  174. typedef struct {
  175. uint8_t month;
  176. uint8_t day;
  177. uint8_t hour;
  178. uint8_t minute;
  179. } sensor_datetime_t;
  180. /* Sensor convert data(a+g) */
  181. typedef struct {
  182. int disable_unitConv; // [input] 0: [acc_data] and [gyro_data] have output
  183. // 1: [acc_data] and [gyro_data] no output
  184. int16_t acc_raw[3]; // [input] sensor hw original output data (axis un-adjusted)
  185. int16_t gyro_raw[3]; // [input] sensor hw original output data (axis un-adjusted)
  186. int16_t acc_org[3]; // [output] sensor hw original output data (axis adjusted)
  187. int16_t gyro_org[3]; // [output] sensor hw original output data (axis adjusted)
  188. float acc_data[3]; // [output] unitConv to unit: m/s^2 (axis adjusted)
  189. float gyro_data[3]; // [output] unitConv to unit: rad/s (axis adjusted)
  190. } sensor_cvt_ag_t;
  191. /* Sensor algorithm result */
  192. typedef struct {
  193. /* orientation values are in degrees */
  194. float orientation;
  195. /* pressure in hectopascal (hPa) */
  196. float pressure;
  197. /* altitude in meter (m) */
  198. float altitude;
  199. /* temperature is in degrees centigrade (Celsius) */
  200. float temperature;
  201. /* heart reate in ppm */
  202. float heart_rate;
  203. /* gnss data */
  204. struct gnss_s {
  205. float latitude; // degrees
  206. float longitude; // degrees
  207. float altitude; // meters
  208. float bearing; // heading in degrees
  209. float speed; // m/s
  210. } gnss;
  211. /* on-body/off-body status */
  212. uint32_t onbody; //1:on-body 0:off-body
  213. /* sedentary status */
  214. //uint32_t sedentary; //1:enter 2:exit 3:remider
  215. /* handup status */
  216. uint32_t handup; //1:hand-up 2:hand-down
  217. /* sleeping data */
  218. struct sleeping_s {
  219. // status 0:enter 1:shallow 2:deep 3:awake 4:exit 12:rapid eye movement
  220. uint32_t status;
  221. // time [month-day-hour-minute]
  222. sensor_datetime_t time_enter;
  223. sensor_datetime_t time_shallow;
  224. sensor_datetime_t time_deep;
  225. sensor_datetime_t time_awake;
  226. sensor_datetime_t time_rem;
  227. sensor_datetime_t time_exit;
  228. // duration in minute
  229. uint32_t duration_total;
  230. uint32_t duration_shallow;
  231. uint32_t duration_deep;
  232. uint32_t duration_awake;
  233. uint32_t duration_rem;
  234. } sleeping;
  235. /* activity mode */
  236. uint32_t activity_mode;
  237. /* activity detect type */
  238. uint32_t activity_detect;
  239. /* pedometer data */
  240. struct pedometer_s {
  241. // 1:static 2:walk 4:upstairs 5:downstairs 6:run 7:bike 8:vehicle
  242. uint32_t activity_type;
  243. float total_steps; // steps
  244. float total_distance; // meters
  245. float total_calories; // Kcal
  246. float cur_step_freq; // steps/minute
  247. float avg_step_freq; // steps/minute
  248. float max_step_freq; // steps/minute
  249. float cur_step_len; // meters
  250. float avg_step_len; // meters
  251. float cur_pace; // minutes/Km
  252. float avg_pace; // minutes/Km
  253. float max_pace; // minutes/Km
  254. float slope; // percentage
  255. float elevation; // meters
  256. float elevation_up; // meters
  257. float elevation_down; // meters
  258. float floors_up; // floors
  259. float floors_down; // floors
  260. float vo2max; // ml/kg/min
  261. float hr_intensity; //
  262. float hr_zone; //
  263. } pedometer;
  264. /* biking data */
  265. struct biking_s {
  266. float latitude; // degrees
  267. float longitude; // degrees
  268. float distance; // meters
  269. float calories; // Kcal
  270. float cur_speed; // km/hour
  271. float avg_speed; // km/hour
  272. float max_speed; // km/hour
  273. float slope; // percentage
  274. float elevation; // meters
  275. float vo2max; // ml/kg/min
  276. float hr_intensity; //
  277. float hr_zone; //
  278. } biking;
  279. /*workout machine*/
  280. struct workout_machine_s {
  281. float counts; // event counts
  282. float calories; // Kcal
  283. float freq; // steps/minute
  284. float avg_freq; // steps/minute
  285. } machine;
  286. struct swimming_s {
  287. float total_distance; // meters
  288. float total_calories; // Kcal
  289. float strokes;
  290. float laps;
  291. float swim_time;
  292. float stroke_freq;
  293. float is_pause;
  294. } swimming;
  295. struct sedentary_s {
  296. uint8_t type; // 1:sedentary; 2:exit sedentary; 3: reminder; 9:stand
  297. uint8_t stand_count;
  298. } sedentary;
  299. } sensor_res_t;
  300. /* Sensor event handler */
  301. typedef void (*sensor_handler_t)(sensor_evt_t *evt);
  302. /* Sensor os api */
  303. typedef struct {
  304. /* User handler */
  305. void (*user_handler)(sensor_evt_t *evt);
  306. } sensor_os_api_t;
  307. /******************************************************************************/
  308. //function
  309. /******************************************************************************/
  310. /* Init sensor algo */
  311. extern int sensor_algo_init(const sensor_os_api_t *api);
  312. /* Control sensor algo */
  313. extern int sensor_algo_control(uint32_t ctl_id, sensor_ctl_t *ctl);
  314. /* Enable sensor id */
  315. extern int sensor_algo_enable(uint32_t id, uint32_t func);
  316. /* Disable sensor id */
  317. extern int sensor_algo_disable(uint32_t id, uint32_t func);
  318. /* Input sensor raw data */
  319. extern void sensor_algo_input(sensor_raw_t* dat);
  320. /* Input sensor fifo init */
  321. extern void sensor_algo_input_fifo_init(int num);
  322. /* Input sensor fifo start */
  323. extern int sensor_algo_input_fifo_start(uint32_t id, uint64_t timestamp, uint64_t delta);
  324. /* Input sensor fifo end */
  325. extern void sensor_algo_input_fifo_end(uint32_t id);
  326. /* Get the duration time of the next event */
  327. extern int64_t sensor_algo_get_next_duration(void);
  328. /* Read sensor data and output through sensor call-back function */
  329. extern int sensor_algo_process(uint32_t id);
  330. /* Read sensor algorithm result */
  331. extern sensor_res_t* sensor_algo_get_result(void);
  332. /* Dump sensor algorithum event */
  333. extern void sensor_algo_dump_event(sensor_evt_t *evt);
  334. /* Dump sensor algorithum result */
  335. extern void sensor_algo_dump_result(sensor_res_t *res);
  336. /* Convert sensor raw data (cywee DML) */
  337. extern void sensor_algo_convert_data(sensor_cvt_ag_t* dat);
  338. /* Get sensor data period(ms) (cywee DML) */
  339. extern int sensor_algo_get_data_period(uint32_t id);
  340. #endif /* _SENSOR_ALGO_H */