forked from vedderb/vesc_bms_fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.h
55 lines (45 loc) · 1.88 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Copyright 2019 - 2020 Benjamin Vedder [email protected]
This file is part of the VESC BMS firmware.
The VESC BMS firmware is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The VESC BMS firmware is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef UTILS_H_
#define UTILS_H_
#include "datatypes.h"
#include <stdint.h>
#include <stdbool.h>
// Functions
int utils_middle_of_3_int(int a, int b, int c);
uint32_t utils_crc32c(uint8_t *data, uint32_t len);
const char* utils_fault_to_string(bms_fault_code fault);
const char* utils_hw_type_to_string(HW_TYPE hw);
float utils_map(float x, float in_min, float in_max, float out_min, float out_max);
int utils_map_int(int x, int in_min, int in_max, int out_min, int out_max);
int utils_truncate_number(float *number, float min, float max);
int utils_truncate_number_int(int *number, int min, int max);
float utils_batt_liion_norm_v_to_capacity(float norm_v);
/**
* A simple low pass filter.
*
* @param value
* The filtered value.
*
* @param sample
* Next sample.
*
* @param filter_constant
* Filter constant. Range 0.0 to 1.0, where 1.0 gives the unfiltered value.
*/
#define UTILS_LP_FAST(value, sample, filter_constant) (value -= (filter_constant) * ((value) - (sample)))
// Return the age of a timestamp in seconds
#define UTILS_AGE_S(x) ((float)chVTTimeElapsedSinceX(x) / (float)CH_CFG_ST_FREQUENCY)
#endif /* UTILS_H_ */