Skip to content

Commit eea7ef0

Browse files
author
Bjoern Schmidt
committed
sensor speed refactoring
1 parent 7eb37c0 commit eea7ef0

File tree

8 files changed

+16
-15
lines changed

8 files changed

+16
-15
lines changed

ACAcommons.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ void updateSpeeds(void) {
9494
ui16_time_ticks_between_speed_interrupt = ui16_time_ticks_for_speed_calculation; //save recent speed
9595
ui16_time_ticks_for_speed_calculation = 0; //reset speed counter
9696

97-
ui32_SPEED_km_h_accumulated -= ui32_SPEED_km_h_accumulated >> 2;
98-
ui32_SPEED_km_h_accumulated += (wheel_circumference * PWM_CYCLES_SECOND * 36L) / (10L * (uint32_t) ui16_time_ticks_between_speed_interrupt); // speed km/h*1000 from external sensor
99-
ui32_SPEED_km_h = ui32_SPEED_km_h_accumulated >> 2; //calculate speed in m/h conversion from sec to hour --> *3600, conversion from mm to km --> /1000000, tic frequency 15625 Hz
97+
ui32_speed_sensor_rpks_accumulated -= ui32_speed_sensor_rpks_accumulated >> 2;
98+
ui32_speed_sensor_rpks_accumulated += (((uint32_t)PWM_CYCLES_SECOND)*1000) / ((uint32_t) ui16_time_ticks_between_speed_interrupt); // speed in rounds per 1000 seconds
99+
ui32_speed_sensor_rpks = ui32_speed_sensor_rpks_accumulated >> 2; //tic frequency 15625 Hz
100100
}
101101

102102
ui8_SPEED_Flag = 0; //reset interrupt flag
@@ -106,7 +106,7 @@ void updateSpeeds(void) {
106106
// FIXME, the following is gathered from two places that were executed just in that order
107107
// distinction 40000/65529 doesn't really make much sense
108108
if (ui16_time_ticks_for_speed_calculation > 40000) {
109-
ui32_SPEED_km_h = 0;
109+
ui32_speed_sensor_rpks = 0;
110110
}
111111
if (ui16_time_ticks_for_speed_calculation > 65529 && ui16_time_ticks_between_speed_interrupt != 65530) {
112112
ui16_time_ticks_between_speed_interrupt = 65530; //Set Display to 0 km/h

ACAcontrollerState.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ uint8_t ui8_hall_order_counter = 5;
104104

105105
uint16_t ui16_speed_kph_to_erps_ratio = 0;
106106

107-
uint32_t ui32_SPEED_km_h; //global variable Speed
108-
uint32_t ui32_SPEED_km_h_accumulated;
107+
uint32_t ui32_speed_sensor_rpks; //speed sensor rounds per 1000 sec
108+
uint16_t ui16_SPEED_km_h_accumulated;
109109
uint16_t ui16_time_ticks_between_speed_interrupt = 64000L; //speed in timetics
110110
uint16_t ui16_time_ticks_for_speed_calculation = 0; //time tics for speed measurement
111111
uint16_t ui16_time_ticks_for_uart_timeout = 0;

ACAcontrollerState.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ extern uint8_t ui8_hall_order_counter;
7979
extern uint8_t ui8_gear_ratio;
8080
extern uint16_t ui16_speed_kph_to_erps_ratio;
8181

82-
extern uint32_t ui32_SPEED_km_h; //global variable Speed
83-
extern uint32_t ui32_SPEED_km_h_accumulated;
82+
extern uint32_t ui32_speed_sensor_rpks; //speed sensor rounds per 1000 sec
83+
extern uint32_t ui32_speed_sensor_rpks_accumulated;
8484

8585
extern uint16_t ui16_time_ticks_for_uart_timeout;
8686
extern uint16_t ui16_time_ticks_for_speed_calculation;

ACAsetPoint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ BitStatus checkOverVoltageOverride(){
9797
uint16_t aca_setpoint(uint16_t ui16_time_ticks_between_speed_interrupt, uint16_t ui16_time_ticks_between_pas_interrupt, uint16_t setpoint_old) {
9898
// select virtual erps speed based on speedsensor type
9999
if (((ui16_aca_flags & EXTERNAL_SPEED_SENSOR) == EXTERNAL_SPEED_SENSOR)) {
100-
ui16_virtual_erps_speed = ui16_speed_kph_to_erps_ratio * ((uint16_t) (ui32_SPEED_km_h / 100)) / 1000; // /100/1000 instead of more plausible /1000/100 cause of 16bit overflow
100+
ui16_virtual_erps_speed = (uint16_t) ((((uint32_t)ui8_gear_ratio) * ui32_speed_sensor_rpks) /1000);
101101
}else{
102102
ui16_virtual_erps_speed = (uint16_t) ui32_erps_filtered;
103103
}

BOdisplay.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ void addBasicStateInfos(void) {
228228
addPayload(CODE_BATTERY_VOLTAGE, ui8_BatteryVoltage);
229229
addPayload(CODE_ER_SPEED_HIGH_BYTE, ui16_motor_speed_erps >> 8);
230230
addPayload(CODE_ER_SPEED, ui16_motor_speed_erps);
231-
addPayload(CODE_SPEED_HIGH_BYTE, ui32_SPEED_km_h >> 8);
232-
addPayload(CODE_SPEED, ui32_SPEED_km_h);
231+
addPayload(CODE_SENSOR_RPKS_HIGH_BYTE, ui32_speed_sensor_rpks >> 8);
232+
addPayload(CODE_SENSOR_RPKS, ui32_speed_sensor_rpks);
233233
addPayload(CODE_BATTERY_CURRENT_HIGH_BYTE, ui16_BatteryCurrent >> 8);
234234
addPayload(CODE_BATTERY_CURRENT, ui16_BatteryCurrent);
235235
addPayload(CODE_SUM_TORQUE, ui16_sum_torque);

BOdisplay.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ typedef enum {
8383
CODE_CORRECTION_VALUE = ((uint8_t) 0xC6),
8484
CODE_PHASE_CURRENT = ((uint8_t) 0xC7),
8585

86-
CODE_SPEED_HIGH_BYTE = ((uint8_t) 0xC8),
87-
CODE_SPEED = ((uint8_t) 0xC9),
86+
CODE_SENSOR_RPKS_HIGH_BYTE = ((uint8_t) 0xC8),
87+
CODE_SENSOR_RPKS = ((uint8_t) 0xC9),
8888

8989
CODE_ER_SPEED_HIGH_BYTE = ((uint8_t) 0xCA),
9090
CODE_ER_SPEED = ((uint8_t) 0xCB),

main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ int main(void) {
189189

190190
//printf("erps %d, motorstate %d, cyclecountertotal %d\r\n", ui16_motor_speed_erps, ui8_motor_state, ui16_PWM_cycles_counter_total);
191191

192-
//printf("cheatstate, %d, km/h %lu, Voltage, %d, setpoint %d, erps %d, current %d, correction_value, %d\n", ui8_offroad_state, ui32_SPEED_km_h, ui8_BatteryVoltage, ui16_setpoint, ui16_motor_speed_erps, ui16_BatteryCurrent, ui8_position_correction_value);
192+
//printf("cheatstate, %d, km/h %lu, Voltage, %d, setpoint %d, erps %d, current %d, correction_value, %d\n", ui8_offroad_state, ui32_speed_sensor_rpks, ui8_BatteryVoltage, ui16_setpoint, ui16_motor_speed_erps, ui16_BatteryCurrent, ui8_position_correction_value);
193193

194194
//printf("kv %d, erps %d, R %d\n", (uint16_t)(float_kv*10.0) , ui16_motor_speed_erps, (uint16_t)(float_R*1000.0));
195195

@@ -203,7 +203,7 @@ int main(void) {
203203
//printf("%d, %d, %d, %d, %d, %d, %d,\r\n", ui8_position_correction_value, ui16_BatteryCurrent, ui16_setpoint, ui8_regen_throttle, ui16_motor_speed_erps, ui16_ADC_iq_current>>2,ui16_adc_read_battery_voltage());
204204

205205

206-
//printf("correction angle %d, Current %d, Voltage %d, sumtorque %d, setpoint %d, km/h %lu\n",ui8_position_correction_value, i16_deziAmps, ui8_BatteryVoltage, ui16_sum_throttle, ui16_setpoint, ui32_SPEED_km_h);
206+
//printf("correction angle %d, Current %d, Voltage %d, sumtorque %d, setpoint %d, km/h %lu\n",ui8_position_correction_value, i16_deziAmps, ui8_BatteryVoltage, ui16_sum_throttle, ui16_setpoint, ui32_speed_sensor_rpks);
207207
#endif
208208
}//end of very slow loop
209209

nbproject/private/private.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<file>file:/G:/osebikefw/main.c</file>
3535
<file>file:/G:/osebikefw/adc.h</file>
3636
<file>file:/G:/osebikefw/ACAcontrollerState.c</file>
37+
<file>file:/G:/osebikefw/BOdisplay.h</file>
3738
<file>file:/G:/osebikefw/display_kingmeter.c</file>
3839
<file>file:/G:/osebikefw/ACAsetPoint.c</file>
3940
<file>file:/G:/osebikefw/ACAcontrollerState.h</file>

0 commit comments

Comments
 (0)