Skip to content

Commit f810939

Browse files
committedJun 15, 2021
Added event logging
1 parent 75b84f1 commit f810939

File tree

7 files changed

+172
-3
lines changed

7 files changed

+172
-3
lines changed
 

‎Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ CSRC = $(STARTUPSRC) \
160160
mempools.c \
161161
worker.c \
162162
bms.c \
163+
events.c \
163164
$(HWSRC) \
164165
$(APPSRC) \
165166
$(NRFSRC) \

‎conf_general.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#define FW_VERSION_MAJOR 5
2525
#define FW_VERSION_MINOR 03
2626
// Set to 0 for building a release and iterate during beta test builds
27-
#define FW_TEST_VERSION_NUMBER 39
27+
#define FW_TEST_VERSION_NUMBER 40
2828

2929
#include "datatypes.h"
3030

‎events.c

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
Copyright 2021 Benjamin Vedder benjamin@vedder.se
3+
4+
This file is part of the VESC firmware.
5+
6+
The VESC firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
The VESC firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#include "events.h"
21+
#include "terminal.h"
22+
#include "commands.h"
23+
#include "utils.h"
24+
#include "ch.h"
25+
#include <string.h>
26+
#include <math.h>
27+
28+
// Settings
29+
#define EVENTS_LEN 30
30+
31+
// Private types
32+
typedef struct {
33+
const char *name;
34+
thread_t *thread;
35+
float param;
36+
systime_t time;
37+
bool set;
38+
} event_t;
39+
40+
// Private variables
41+
static volatile event_t m_events[EVENTS_LEN];
42+
static volatile int m_event_now = 0;
43+
static mutex_t m_mtx;
44+
45+
// Private functions
46+
static void terminal_print(int argc, const char **argv);
47+
48+
void events_init(void) {
49+
chMtxObjectInit(&m_mtx);
50+
51+
for (int i = 0;i < EVENTS_LEN;i++) {
52+
volatile event_t *e = &m_events[i];
53+
e->set = false;
54+
}
55+
56+
terminal_register_command_callback(
57+
"events",
58+
"Print recent motor events",
59+
0,
60+
terminal_print);
61+
}
62+
63+
void events_add(const char *name, float param) {
64+
chMtxLock(&m_mtx);
65+
66+
int event = m_event_now;
67+
68+
event--;
69+
if (event < 0) {
70+
event = EVENTS_LEN - 1;
71+
}
72+
volatile event_t *e = &m_events[event];
73+
74+
// Just update the last event if it looks like
75+
// a repeated command. Otherwise the buffer will
76+
// fill too fast.
77+
if (e->name != name || // Comparing memory location is enough
78+
e->thread != chThdGetSelfX() ||
79+
UTILS_AGE_S(e->time) > 0.2 ||
80+
(fabsf(param) > 1e-4) != (fabsf(e->param) > 1e-4)) {
81+
event = (event + 1) % EVENTS_LEN;
82+
e = &m_events[event];
83+
}
84+
85+
e->name = name;
86+
e->thread = chThdGetSelfX();
87+
e->param = param;
88+
e->time = chVTGetSystemTimeX();
89+
e->set = true;
90+
91+
event = (event + 1) % EVENTS_LEN;
92+
m_event_now = event;
93+
94+
chMtxUnlock(&m_mtx);
95+
}
96+
97+
static void terminal_print(int argc, const char **argv) {
98+
(void)argc; (void)argv;
99+
100+
int event = m_event_now;
101+
int print_cnt = 0;
102+
103+
do {
104+
volatile event_t *e = &m_events[event];
105+
106+
if (e->set) {
107+
print_cnt++;
108+
commands_printf("Age : %.2f s", (double)UTILS_AGE_S(e->time));
109+
commands_printf("Thread : %s", e->thread->p_name);
110+
commands_printf("Motor : %i", e->thread->motor_selected);
111+
commands_printf("Command: %s", e->name);
112+
commands_printf("Param : %.3f\n", (double)e->param);
113+
}
114+
115+
event = (event + 1) % EVENTS_LEN;
116+
} while (event != m_event_now);
117+
118+
commands_printf("Events total: %d\n", print_cnt);
119+
}

‎events.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright 2021 Benjamin Vedder benjamin@vedder.se
3+
4+
This file is part of the VESC firmware.
5+
6+
The VESC firmware is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
The VESC firmware is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef EVENTS_H_
21+
#define EVENTS_H_
22+
23+
#include <stdbool.h>
24+
#include <stdint.h>
25+
26+
void events_init(void);
27+
void events_add(const char *name, float param);
28+
29+
#endif /* EVENTS_H_ */

‎main.c

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#endif
5454
#include "shutdown.h"
5555
#include "mempools.h"
56+
#include "events.h"
5657

5758
/*
5859
* HW resources used:
@@ -205,6 +206,7 @@ int main(void) {
205206

206207
chThdSleepMilliseconds(100);
207208

209+
events_init();
208210
hw_init_gpio();
209211
LED_RED_OFF();
210212
LED_GREEN_OFF();

‎mc_interface.c

+17
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "mempools.h"
3939
#include "crc.h"
4040
#include "bms.h"
41+
#include "events.h"
4142

4243
#include <math.h>
4344
#include <stdlib.h>
@@ -589,6 +590,8 @@ void mc_interface_set_duty(float dutyCycle) {
589590
default:
590591
break;
591592
}
593+
594+
events_add("set_duty", dutyCycle);
592595
}
593596

594597
void mc_interface_set_duty_noramp(float dutyCycle) {
@@ -613,6 +616,8 @@ void mc_interface_set_duty_noramp(float dutyCycle) {
613616
default:
614617
break;
615618
}
619+
620+
events_add("set_duty_noramp", dutyCycle);
616621
}
617622

618623
void mc_interface_set_pid_speed(float rpm) {
@@ -637,6 +642,8 @@ void mc_interface_set_pid_speed(float rpm) {
637642
default:
638643
break;
639644
}
645+
646+
events_add("set_pid_speed", rpm);
640647
}
641648

642649
void mc_interface_set_pid_pos(float pos) {
@@ -664,6 +671,8 @@ void mc_interface_set_pid_pos(float pos) {
664671
default:
665672
break;
666673
}
674+
675+
events_add("set_pid_pos", pos);
667676
}
668677

669678
void mc_interface_set_current(float current) {
@@ -688,6 +697,8 @@ void mc_interface_set_current(float current) {
688697
default:
689698
break;
690699
}
700+
701+
events_add("set_current", current);
691702
}
692703

693704
void mc_interface_set_brake_current(float current) {
@@ -717,6 +728,8 @@ void mc_interface_set_brake_current(float current) {
717728
default:
718729
break;
719730
}
731+
732+
events_add("set_current_brake", current);
720733
}
721734

722735
/**
@@ -776,6 +789,8 @@ void mc_interface_set_handbrake(float current) {
776789
default:
777790
break;
778791
}
792+
793+
events_add("set_handbrake", current);
779794
}
780795

781796
/**
@@ -819,6 +834,8 @@ void mc_interface_release_motor_override(void) {
819834
default:
820835
break;
821836
}
837+
838+
events_add("release_motor_override", 0.0);
822839
}
823840

824841
bool mc_interface_wait_for_motor_release(float timeout) {

‎timeout.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,14 @@ static THD_FUNCTION(timeout_thread, arg) {
209209
}
210210

211211
if (kill_sw || (timeout_msec != 0 && chVTTimeElapsedSinceX(last_update_time) > MS2ST(timeout_msec))) {
212-
mc_interface_release_motor_override();
212+
if (!has_timeout && !kill_sw_active) {
213+
mc_interface_release_motor_override();
214+
}
213215
mc_interface_unlock();
214216
mc_interface_select_motor_thread(1);
215217
mc_interface_set_brake_current(timeout_brake_current);
216218
mc_interface_select_motor_thread(2);
217219
mc_interface_set_brake_current(timeout_brake_current);
218-
mc_interface_ignore_input(20);
219220

220221
if (!kill_sw) {
221222
has_timeout = true;

0 commit comments

Comments
 (0)
Please sign in to comment.