@@ -54,6 +54,7 @@ volatile float ADC_curr_norm_value[6];
54
54
typedef struct {
55
55
volatile mc_configuration m_conf ;
56
56
mc_fault_code m_fault_now ;
57
+ setup_stats m_stats ;
57
58
int m_ignore_iterations ;
58
59
int m_drv_fault_iterations ;
59
60
unsigned int m_cycles_running ;
@@ -124,6 +125,7 @@ static volatile bool m_fault_stop_is_second_motor;
124
125
// Private functions
125
126
static void update_override_limits (volatile motor_if_state_t * motor , volatile mc_configuration * conf );
126
127
static void run_timer_tasks (volatile motor_if_state_t * motor );
128
+ static void update_stats (volatile motor_if_state_t * motor );
127
129
static volatile motor_if_state_t * motor_now (void );
128
130
129
131
// Function pointers
@@ -138,6 +140,8 @@ static thread_t *sample_send_tp;
138
140
static THD_WORKING_AREA (fault_stop_thread_wa , 512 ) ;
139
141
static THD_FUNCTION (fault_stop_thread , arg ) ;
140
142
static thread_t * fault_stop_tp ;
143
+ static THD_WORKING_AREA (stat_thread_wa , 512 ) ;
144
+ static THD_FUNCTION (stat_thread , arg ) ;
141
145
142
146
void mc_interface_init (void ) {
143
147
memset ((void * )& m_motor_1 , 0 , sizeof (motor_if_state_t ));
@@ -165,10 +169,13 @@ void mc_interface_init(void) {
165
169
m_sample_mode_last = DEBUG_SAMPLING_OFF ;
166
170
m_sample_is_second_motor = false;
167
171
172
+ mc_interface_stat_reset ();
173
+
168
174
// Start threads
169
175
chThdCreateStatic (timer_thread_wa , sizeof (timer_thread_wa ), NORMALPRIO , timer_thread , NULL );
170
176
chThdCreateStatic (sample_send_thread_wa , sizeof (sample_send_thread_wa ), NORMALPRIO - 1 , sample_send_thread , NULL );
171
177
chThdCreateStatic (fault_stop_thread_wa , sizeof (fault_stop_thread_wa ), HIGHPRIO - 3 , fault_stop_thread , NULL );
178
+ chThdCreateStatic (stat_thread_wa , sizeof (stat_thread_wa ), NORMALPRIO , stat_thread , NULL );
172
179
173
180
int motor_old = mc_interface_get_motor_thread ();
174
181
mc_interface_select_motor_thread (1 );
@@ -2538,6 +2545,121 @@ static THD_FUNCTION(timer_thread, arg) {
2538
2545
}
2539
2546
}
2540
2547
2548
+ static void update_stats (volatile motor_if_state_t * motor ) {
2549
+ mc_interface_select_motor_thread (motor == (& m_motor_1 ) ? 1 : 2 );
2550
+
2551
+ setup_values val = mc_interface_get_setup_values ();
2552
+
2553
+ const double power = mc_interface_get_input_voltage_filtered () * fabsf (val .current_in_tot );
2554
+ const double speed = mc_interface_get_speed ();
2555
+ const double temp_mos = mc_interface_temp_fet_filtered ();
2556
+ const double temp_mot = mc_interface_temp_motor_filtered ();
2557
+
2558
+ motor -> m_stats .power_sum += power ;
2559
+ motor -> m_stats .speed_sum += fabs (speed );
2560
+ motor -> m_stats .temp_mos_sum += temp_mos ;
2561
+ motor -> m_stats .temp_motor_sum += temp_mot ;
2562
+ motor -> m_stats .current_sum += fabs ((double )(val .current_tot ));
2563
+ motor -> m_stats .samples += (double )1.0 ;
2564
+
2565
+ if (power > (double )motor -> m_stats .max_power ) {
2566
+ motor -> m_stats .max_power = power ;
2567
+ }
2568
+
2569
+ if (fabs (speed ) > (double )motor -> m_stats .max_speed ) {
2570
+ motor -> m_stats .max_speed = fabsf (speed );
2571
+ }
2572
+
2573
+ if (temp_mos > (double )motor -> m_stats .max_temp_mos ) {
2574
+ motor -> m_stats .max_temp_mos = temp_mos ;
2575
+ }
2576
+
2577
+ if (temp_mot > (double )motor -> m_stats .max_temp_motor ) {
2578
+ motor -> m_stats .max_temp_motor = temp_mot ;
2579
+ }
2580
+
2581
+ if (fabsf (val .current_tot ) > motor -> m_stats .max_current ) {
2582
+ motor -> m_stats .max_current = fabsf (val .current_tot );
2583
+ }
2584
+ }
2585
+
2586
+ float mc_interface_stat_speed_avg (void ) {
2587
+ volatile setup_stats * s = & motor_now ()-> m_stats ;
2588
+ double res = s -> speed_sum / s -> samples ;
2589
+ return res ;
2590
+ }
2591
+
2592
+ float mc_interface_stat_speed_max (void ) {
2593
+ return motor_now ()-> m_stats .max_speed ;
2594
+ }
2595
+
2596
+ float mc_interface_stat_power_avg (void ) {
2597
+ volatile setup_stats * s = & motor_now ()-> m_stats ;
2598
+ double res = s -> power_sum / s -> samples ;
2599
+ return res ;
2600
+ }
2601
+
2602
+ float mc_interface_stat_power_max (void ) {
2603
+ return motor_now ()-> m_stats .max_power ;
2604
+ }
2605
+
2606
+ float mc_interface_stat_current_avg (void ) {
2607
+ volatile setup_stats * s = & motor_now ()-> m_stats ;
2608
+ double res = s -> current_sum / s -> samples ;
2609
+ return res ;
2610
+ }
2611
+
2612
+ float mc_interface_stat_current_max (void ) {
2613
+ return motor_now ()-> m_stats .max_current ;
2614
+ }
2615
+
2616
+ float mc_interface_stat_temp_mosfet_avg (void ) {
2617
+ volatile setup_stats * s = & motor_now ()-> m_stats ;
2618
+ double res = s -> temp_mos_sum / s -> samples ;
2619
+ return res ;
2620
+ }
2621
+
2622
+ float mc_interface_stat_temp_mosfet_max (void ) {
2623
+ return motor_now ()-> m_stats .max_temp_mos ;
2624
+ }
2625
+
2626
+ float mc_interface_stat_temp_motor_avg (void ) {
2627
+ volatile setup_stats * s = & motor_now ()-> m_stats ;
2628
+ double res = s -> temp_motor_sum / s -> samples ;
2629
+ return res ;
2630
+ }
2631
+
2632
+ float mc_interface_stat_temp_motor_max (void ) {
2633
+ return motor_now ()-> m_stats .max_temp_motor ;
2634
+ }
2635
+
2636
+ float mc_interface_stat_count_time (void ) {
2637
+ return UTILS_AGE_S (motor_now ()-> m_stats .time_start );
2638
+ }
2639
+
2640
+ void mc_interface_stat_reset (void ) {
2641
+ volatile setup_stats * s = & motor_now ()-> m_stats ;
2642
+ memset ((void * )s , 0 , sizeof (setup_stats ));
2643
+ s -> time_start = chVTGetSystemTimeX ();
2644
+ s -> max_temp_mos = -300.0 ;
2645
+ s -> max_temp_motor = -300.0 ;
2646
+ }
2647
+
2648
+ static THD_FUNCTION (stat_thread , arg ) {
2649
+ (void )arg ;
2650
+
2651
+ chRegSetThreadName ("StatCounter" );
2652
+
2653
+ for (;;) {
2654
+ update_stats (& m_motor_1 );
2655
+ #ifdef HW_HAS_DUAL_MOTORS
2656
+ update_stats (& m_motor_2 );
2657
+ #endif
2658
+
2659
+ chThdSleepMilliseconds (10 );
2660
+ }
2661
+ }
2662
+
2541
2663
static THD_FUNCTION (sample_send_thread , arg ) {
2542
2664
(void )arg ;
2543
2665
0 commit comments