Skip to content

Commit ca89c76

Browse files
committed
Issue #20: Fix overflow 16-bit timers, extern timers and integrate with parameter database
1 parent b688c5b commit ca89c76

File tree

4 files changed

+53
-26
lines changed

4 files changed

+53
-26
lines changed

KPI_Rover/Database/ulDatabase.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ enum ulDatabase_ParamType {
1717
};
1818

1919
enum ulDatabase_ParamId {
20-
PARAM_MOTOR_P_GAIN,
21-
PARAM_HEADING_OFFSET,
22-
PARAM_BATTERY_LEVEL,
23-
PARAM_ENCODER_COUNT,
20+
MOTOR_FL_RPM,
21+
MOTOR_FR_RPM,
22+
MOTOR_RL_RPM,
23+
MOTOR_RR_RPM,
24+
ENCODER_CONTROL_PERIOD_MS,
2425
PARAM_COUNT
2526
};
2627

KPI_Rover/Encoders/drvEncoder.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
#include "drvEncoder.h"
22

33

4+
extern TIM_HandleTypeDef htim1;
45
extern TIM_HandleTypeDef htim2;
5-
/*other encoders
6-
extern TIM_HandleTypeDef htim?;
7-
extern TIM_HandleTypeDef htim?;
8-
extern TIM_HandleTypeDef htim?;
9-
*/
6+
extern TIM_HandleTypeDef htim3;
7+
extern TIM_HandleTypeDef htim4;
8+
109

1110
void drvEncoder_Init(void) {
11+
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);
1212
HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
13-
/* other encoders
14-
HAL_TIM_Encoder_Start(&htim?, TIM_CHANNEL_ALL);
15-
HAL_TIM_Encoder_Start(&htim?, TIM_CHANNEL_ALL);
16-
HAL_TIM_Encoder_Start(&htim?, TIM_CHANNEL_ALL);
17-
*/
13+
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
14+
HAL_TIM_Encoder_Start(&htim4, TIM_CHANNEL_ALL);
1815
}
1916

2017
uint32_t drvEncoder_Read(uint8_t channel) {
2118
switch (channel) {
22-
case 0: return TIM2->CNT;
23-
/* other encoders
24-
case 1: return TIM?->CNT;
25-
case 2: return TIM?->CNT;
26-
case 3: return TIM?->CNT;
27-
*/
19+
case 0: return TIM1->CNT;
20+
case 1: return TIM2->CNT;
21+
case 2: return TIM3->CNT;
22+
case 3: return TIM4->CNT;
2823
default: return 0;
2924
}
3025
}

KPI_Rover/Encoders/ulEncoder.c

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,53 @@
11
#include "ulEncoder.h"
22
#include "drvEncoder.h"
3+
#include "Database/ulDatabase.h"
34

45
#include "FreeRTOS.h"
56
#include "cmsis_os2.h"
67

78

89
#define TICKS_PER_REVOLUTION 820.0f
9-
#define MOTORS_CONTROL_PERIOD_MS 20
1010
#define MOTORS_COUNT 4
1111

12+
#define TIMER_16_BIT_MAX 0x10000
13+
#define OVERFLOW_THRESHOLD TIMER_16_BIT_MAX / 2
14+
1215

1316
static uint32_t lastTicks_RPM[MOTORS_COUNT];
1417
static uint32_t lastTicks_Ethernet[MOTORS_COUNT];
1518

19+
static uint16_t encoder_period_ms = 5;
20+
1621
static osTimerId_t encoderTimerHandle;
1722

1823

24+
static inline int32_t ulEncoder_CalculateDiff(uint32_t current, uint32_t last) {
25+
int32_t diff = (int32_t)(current - last);
26+
27+
// overflow in 16-bit timer
28+
if (diff < -OVERFLOW_THRESHOLD) {
29+
diff += TIMER_16_BIT_MAX;
30+
}
31+
else if (diff > OVERFLOW_THRESHOLD) {
32+
diff -= TIMER_16_BIT_MAX;
33+
}
34+
35+
return diff;
36+
}
37+
1938
static void ulEncoder_TimerCallback(void *argument) {
2039

2140
for (int i = 0; i < MOTORS_COUNT; i++) {
2241
uint32_t currentTicks = drvEncoder_Read(i);
2342
uint32_t lastTicks = lastTicks_RPM[i];
2443

25-
int32_t diff = (int32_t) (currentTicks - lastTicks);
44+
int32_t diff = ulEncoder_CalculateDiff(currentTicks, lastTicks);
2645

27-
float ticks_per_sec = (float) diff * (1000.0f / MOTORS_CONTROL_PERIOD_MS);
46+
float ticks_per_sec = (float) diff * (1000.0f / encoder_period_ms);
2847
float rpm = ticks_per_sec * 60.0f/ TICKS_PER_REVOLUTION;
2948

30-
// TODO: adding rpm to the database
49+
int32_t rpm_to_db = (int32_t)rpm;
50+
ulDatabase_setInt32(MOTOR_FL_RPM + i, rpm_to_db);
3151

3252
lastTicks_RPM[i] = currentTicks;
3353
}
@@ -37,6 +57,10 @@ void ulEncoder_Init(void) {
3757

3858
drvEncoder_Init();
3959

60+
if (!ulDatabase_getUint16(ENCODER_CONTROL_PERIOD_MS, &encoder_period_ms) || encoder_period_ms == 0) {
61+
encoder_period_ms = 5;
62+
}
63+
4064
for (int i = 0; i < MOTORS_COUNT; i++) {
4165
uint32_t initialTicks = drvEncoder_Read(i);
4266
lastTicks_RPM[i] = initialTicks;
@@ -50,7 +74,7 @@ void ulEncoder_Init(void) {
5074
encoderTimerHandle = osTimerNew(ulEncoder_TimerCallback, osTimerPeriodic, NULL, &encoderTimer_attributes);
5175

5276
if (encoderTimerHandle != NULL) {
53-
osTimerStart(encoderTimerHandle, MOTORS_CONTROL_PERIOD_MS);
77+
osTimerStart(encoderTimerHandle, encoder_period_ms);
5478
}
5579
}
5680

@@ -61,9 +85,10 @@ void ulEncoder_GetDiffForEthernet(int32_t *diffOutput) {
6185
uint32_t currentTicks = drvEncoder_Read(i);
6286
uint32_t lastTicks = lastTicks_Ethernet[i];
6387

64-
int32_t diff = (int32_t)(currentTicks - lastTicks);
88+
int32_t diff = ulEncoder_CalculateDiff(currentTicks, lastTicks);
6589

6690
diffOutput[i] = diff;
6791
lastTicks_Ethernet[i] = currentTicks;
6892
}
6993
}
94+

KPI_Rover/KPIRover.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55

66

77
static struct ulDatabase_ParamMetadata ulDatabase_params[] = {
8+
{0, INT32, false, 0}, // MOTOR_FL_RPM,
9+
{0, INT32, false, 0}, // MOTOR_FR_RPM,
10+
{0, INT32, false, 0}, // MOTOR_RL_RPM,
11+
{0, INT32, false, 0}, // MOTOR_RR_RPM,
12+
{0, UINT16, true, 5}, // ENCODER_CONTROL_PERIOD_MS,
813
};
914

1015
void KPIRover_Init(void) {
1116
ulDatabase_init(ulDatabase_params, sizeof(ulDatabase_params) / sizeof(struct ulDatabase_ParamMetadata));
17+
ulEncoder_Init();
1218
}

0 commit comments

Comments
 (0)