Le-Guily
Firmware du robot auto-équilibré (Zephyr, STM32WB55)
Chargement...
Recherche...
Aucune correspondance
safety.c
Aller à la documentation de ce fichier.
1
11
12#include <zephyr/kernel.h>
13#include <zephyr/sys/printk.h>
14#include "priorities.h"
15#include "include.h"
16
17static float buffer[10];
18static int index = 0;
19static int count = 0;
20static float sum = 0.0f;
21static float moyenne;
22
23static float moving_average(float new_value) {
24
25 if (count == 10) {
26
27 sum -= buffer[index];
28
29 } else {
30
31 count++;
32 }
33 buffer[index] = new_value;
34
35 sum += new_value;
36
37 index = (index + 1) % 10;
38
39 moyenne = sum / count;
40
41 return moyenne;
42}
43
44static void thread_safety(void *p1, void *p2, void *p3)
45{
46
47 static uint32_t compteur;
48
49 static int32_t mvs = 1500;
50
51 while (1) {
52
53
54 if ((compteur % 10) == 0){
55
56 if (battery_read_mv(&mvs) == 0) {
57
58 moving_average(mvs);
59
60 printk("VBAT = %d mV\n", (int)moyenne);
61 compteur = 0;
62 }
63 }
64
65 compteur++;
66
67 k_msleep(50);
68 }
69}
70
71K_THREAD_DEFINE(thread_safety_id, STACK_SIZE, thread_safety, NULL, NULL, NULL,
72 PRIORITY_SAFETY, 0, 0);
73
int battery_read_mv(int32_t *mv)
Definition battery.c:53
En-tête de regroupement.
Priorités des threads Zephyr et taille de leur pile (plus le nombre est petit, plus le thread est pri...
#define STACK_SIZE
Definition priorities.h:15
#define PRIORITY_SAFETY
Definition priorities.h:11
static void thread_safety(void *p1, void *p2, void *p3)
Definition safety.c:44
static float moyenne
Definition safety.c:21
static int index
Definition safety.c:18
K_THREAD_DEFINE(thread_safety_id, STACK_SIZE, thread_safety, NULL, NULL, NULL, PRIORITY_SAFETY, 0, 0)
static float moving_average(float new_value)
Definition safety.c:23
static float buffer[10]
Definition safety.c:17
static int count
Definition safety.c:19
static float sum
Definition safety.c:20