forked from stancecoke/BMSBattery_S_controllers_firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cruise_control.c
executable file
·68 lines (61 loc) · 1.34 KB
/
cruise_control.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* EGG OpenSource EBike firmware
*
* Copyright (C) Casainho, 2015, 2106, 2017.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include "stm8s.h"
#include "cruise_control.h"
uint8_t ui8_cruise_state = 0;
uint8_t ui8_cruise_value = 0;
uint8_t ui8_cruise_output = 0;
uint32_t ui32_cruise_counter = 0;
uint8_t cruise_control (uint8_t ui8_value)
{
// Cruise control
if (ui8_cruise_state == 0)
{
if ((ui8_value > 25) &&
((ui8_value > (ui8_cruise_value - 25)) || (ui8_value < (ui8_cruise_value + 15))))
{
ui32_cruise_counter++;
ui8_cruise_output = ui8_value;
if (ui32_cruise_counter > 25) // 100 time control: time to lock cruise control
{
ui8_cruise_state = 1;
ui8_cruise_output = ui8_value;
ui32_cruise_counter = 0;
ui8_cruise_value = 0;
}
}
else
{
ui32_cruise_counter = 0;
ui8_cruise_value = ui8_value;
ui8_cruise_output = ui8_cruise_value;
}
}
else if (ui8_cruise_state == 1)
{
if (ui8_value < 25) { ui8_cruise_state = 2; }
}
else if (ui8_cruise_state == 2)
{
if (ui8_value > 25)
{
ui8_cruise_state = 0;
ui8_cruise_output = ui8_value;
}
}
return ui8_cruise_output;
}
void set_cruise_control_state (uint8_t value)
{
ui8_cruise_state = value;
}
void stop_cruise_control (void)
{
set_cruise_control_state (0);
}