Skip to content

Commit 0c4e54a

Browse files
authored
Merge pull request #12 from iralabdisco/communication
Fixes encoder bug
2 parents 293bd17 + 61a8a7d commit 0c4e54a

149 files changed

Lines changed: 41781 additions & 295 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

otto_controller/Core/Inc/communication_utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,8 @@ typedef struct __attribute__((packed)){
3030
int right_ticks;
3131
} ticks_msg;
3232

33+
typedef struct __attribute__((packed)){
34+
float left_vel;
35+
float right_vel;
36+
} wheel_msg;
37+

otto_controller/Core/Inc/constants.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
#define BASELINE 0.3 //distance between wheels in meters
55
#define MAX_DUTY_CYCLE 790
66

7-
#define LEFT_TICKS_PER_METER 195788 //no
8-
#define RIGHT_TICKS_PER_METER 196829 //no
9-
107
#define TICKS_PER_REVOLUTION 148000 //x4 resolution
11-
#define RIGHT_WHEEL_CIRCUMFERENCE 0.8 //in meters
12-
#define LEFT_WHEEL_CIRCUMFERENCE 0.78 //in meters
8+
#define RIGHT_WHEEL_CIRCUMFERENCE 0.783 //in meters
9+
#define LEFT_WHEEL_CIRCUMFERENCE 0.789 //in meters
1310

1411
#endif

otto_controller/Core/Inc/encoder.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,13 @@ class Encoder {
1111
uint32_t current_millis_;
1212
int32_t ticks_; //if negative the wheel is going backwards
1313
float wheel_circumference_;
14-
int ticks_per_meter_;
1514

1615
Encoder() {
1716
timer_ = NULL;
1817
wheel_circumference_ = 0;
19-
ticks_per_meter_ = 0;
2018
}
2119

22-
// Encoder(TIM_HandleTypeDef *timer, float wheel_circ);
23-
Encoder(TIM_HandleTypeDef *timer, int ticks_per_meters);
24-
20+
Encoder(TIM_HandleTypeDef *timer, float wheel_circ);
2521

2622
void Setup();
2723

otto_controller/Core/Src/encoder.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
#include "encoder.h"
22

3-
//Encoder::Encoder(TIM_HandleTypeDef *timer, float wheel_circ) {
4-
// timer_ = timer;
5-
// wheel_circumference_ = wheel_circ;
6-
//
7-
//}
8-
9-
Encoder::Encoder(TIM_HandleTypeDef *timer, int ticks_per_meter) {
3+
Encoder::Encoder(TIM_HandleTypeDef *timer, float wheel_circ) {
104
timer_ = timer;
11-
ticks_per_meter_ = ticks_per_meter;
5+
wheel_circumference_ = wheel_circ;
126

137
}
148

@@ -26,18 +20,12 @@ void Encoder::UpdateValues() {
2620
this->ResetCount();
2721
}
2822

29-
//float Encoder::GetMeters() {
30-
// float meters = ((float) this->ticks_ * this->wheel_circumference_)
31-
// / TICKS_PER_REVOLUTION;
32-
// return meters;
33-
//}
34-
3523
float Encoder::GetMeters() {
36-
float meters = (((float) this->ticks_) / this->ticks_per_meter_);
24+
float meters = ((float) this->ticks_ * this->wheel_circumference_)
25+
/ TICKS_PER_REVOLUTION;
3726
return meters;
3827
}
3928

40-
4129
float Encoder::GetLinearVelocity() {
4230
this->UpdateValues();
4331
float meters = this->GetMeters();

otto_controller/Core/Src/main.cpp

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ UART_HandleTypeDef huart6;
5858
/* USER CODE BEGIN PV */
5959

6060
//Odometry
61-
Encoder right_encoder = Encoder(&htim5, RIGHT_TICKS_PER_METER);
62-
Encoder left_encoder = Encoder(&htim2, LEFT_TICKS_PER_METER);
61+
Encoder right_encoder = Encoder(&htim5, RIGHT_WHEEL_CIRCUMFERENCE);
62+
Encoder left_encoder = Encoder(&htim2, LEFT_WHEEL_CIRCUMFERENCE);
6363
Odometry odom = Odometry();
6464

6565
float left_velocity;
@@ -75,25 +75,25 @@ int left_dutycycle;
7575
int right_dutycycle;
7676

7777
//MotorController
78-
MotorController left_motor(sleep1_GPIO_Port,
78+
MotorController right_motor(sleep1_GPIO_Port,
7979
sleep1_Pin,
80-
dir1_GPIO_Port,
81-
dir1_Pin,
82-
&htim4,
83-
TIM_CHANNEL_4);
84-
MotorController right_motor(sleep2_GPIO_Port,
85-
sleep2_Pin,
86-
dir2_GPIO_Port,
87-
dir2_Pin,
80+
dir1_GPIO_Port,
81+
dir1_Pin,
8882
&htim4,
89-
TIM_CHANNEL_3);
83+
TIM_CHANNEL_4);
84+
MotorController left_motor(sleep2_GPIO_Port,
85+
sleep2_Pin,
86+
dir2_GPIO_Port,
87+
dir2_Pin,
88+
&htim4,
89+
TIM_CHANNEL_3);
9090

9191
//Communication
9292
uint8_t *tx_buffer;
9393
uint8_t *rx_buffer;
9494

95-
odometry_msg odom_msg;
9695
velocity_msg vel_msg;
96+
wheel_msg wheels_msg;
9797

9898
uint8_t mode = 0; //setup mode
9999

@@ -160,18 +160,16 @@ int main(void) {
160160

161161
left_motor.setup();
162162
right_motor.setup();
163+
163164
left_motor.coast();
164165
right_motor.coast();
165166

166-
tx_buffer = (uint8_t*) &odom_msg;
167+
tx_buffer = (uint8_t*) &wheels_msg;
167168
rx_buffer = (uint8_t*) &vel_msg;
168169

169170
//Enables UART RX interrupt
170171
HAL_UART_Receive_IT(&huart6, rx_buffer, 8);
171172

172-
//Enables TIM6 interrupt (used for periodic transmission)
173-
HAL_TIM_Base_Start_IT(&htim6);
174-
175173
/* USER CODE END 2 */
176174

177175
/* Infinite loop */
@@ -193,11 +191,11 @@ void SystemClock_Config(void) {
193191
RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
194192
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = { 0 };
195193

196-
/** Configure the main internal regulator output voltage
194+
/** Configure the main internal regulator output voltage
197195
*/
198196
__HAL_RCC_PWR_CLK_ENABLE();
199197
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
200-
/** Initializes the CPU, AHB and APB busses clocks
198+
/** Initializes the CPU, AHB and APB busses clocks
201199
*/
202200
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
203201
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
@@ -206,7 +204,7 @@ void SystemClock_Config(void) {
206204
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
207205
Error_Handler();
208206
}
209-
/** Initializes the CPU, AHB and APB busses clocks
207+
/** Initializes the CPU, AHB and APB busses clocks
210208
*/
211209
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
212210
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
@@ -237,7 +235,7 @@ static void MX_NVIC_Init(void) {
237235
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 2, 2);
238236
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
239237
/* USART6_IRQn interrupt configuration */
240-
HAL_NVIC_SetPriority(USART6_IRQn, 2, 0);
238+
HAL_NVIC_SetPriority(USART6_IRQn, 1, 0);
241239
HAL_NVIC_EnableIRQ(USART6_IRQn);
242240
}
243241

@@ -481,9 +479,9 @@ static void MX_USART6_UART_Init(void) {
481479
/* USER CODE END USART6_Init 1 */
482480
huart6.Instance = USART6;
483481
huart6.Init.BaudRate = 115200;
484-
huart6.Init.WordLength = UART_WORDLENGTH_9B;
482+
huart6.Init.WordLength = UART_WORDLENGTH_8B;
485483
huart6.Init.StopBits = UART_STOPBITS_1;
486-
huart6.Init.Parity = UART_PARITY_ODD;
484+
huart6.Init.Parity = UART_PARITY_NONE;
487485
huart6.Init.Mode = UART_MODE_TX_RX;
488486
huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
489487
huart6.Init.OverSampling = UART_OVERSAMPLING_16;
@@ -591,14 +589,16 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
591589
left_dutycycle += cross_dutycycle;
592590
right_dutycycle -= cross_dutycycle;
593591

592+
wheels_msg.left_vel = left_velocity;
593+
wheels_msg.right_vel = right_velocity;
594+
594595
}
595596

596597
//TIMER 2Hz Transmit
597598
if (htim->Instance == TIM6) {
598599

599600
//TODO odometry
600-
601-
HAL_UART_Transmit(&huart6, tx_buffer, 8, 100);
601+
HAL_UART_Transmit_IT(&huart6, tx_buffer, 8);
602602
}
603603
}
604604

@@ -625,6 +625,8 @@ void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
625625
mode = 1;
626626
//Enables TIM3 interrupt (used for PID control)
627627
HAL_TIM_Base_Start_IT(&htim3);
628+
//Enables TIM6 interrupt (used for periodic transmission)
629+
HAL_TIM_Base_Start_IT(&htim6);
628630

629631
}
630632

otto_controller/otto_controller.ioc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false
5757
NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false\:true
5858
NVIC.TIM3_IRQn=true\:2\:1\:true\:true\:true\:1\:true\:true
5959
NVIC.TIM6_DAC_IRQn=true\:2\:2\:true\:true\:true\:2\:true\:true
60-
NVIC.USART6_IRQn=true\:2\:0\:true\:true\:true\:3\:true\:true
60+
NVIC.USART6_IRQn=true\:1\:0\:true\:true\:true\:3\:true\:true
6161
NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false
6262
PA0/WKUP.GPIOParameters=GPIO_Label
6363
PA0/WKUP.GPIO_Label=encoder_dx1
@@ -230,9 +230,9 @@ TIM6.IPParameters=Prescaler,Period
230230
TIM6.Period=799
231231
TIM6.Prescaler=9999
232232
USART6.IPParameters=VirtualMode-Asynchronous,WordLength,Parity
233-
USART6.Parity=PARITY_ODD
233+
USART6.Parity=PARITY_NONE
234234
USART6.VirtualMode-Asynchronous=VM_ASYNC
235-
USART6.WordLength=WORDLENGTH_9B
235+
USART6.WordLength=WORDLENGTH_8B
236236
VP_SYS_VS_Systick.Mode=SysTick
237237
VP_SYS_VS_Systick.Signal=SYS_VS_Systick
238238
VP_TIM3_VS_ClockSourceINT.Mode=Internal

utils/.project

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>utils</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
</buildSpec>
9+
<natures>
10+
</natures>
11+
</projectDescription>

utils/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Utils
2+
3+
* catikin_ws contains useful ROS nodes.
4+
* pid_tuning contains Otto code and python scripts for PID tuning
5+
* py_serial_examples contains pyserial_examples
6+
* ticks_calibration contains Otto code and python scripts for ticks calibration

utils/catkin_ws/src/joypad_bridge/scripts/cmd_vel_transmitter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
ser = serial.Serial(
88
baudrate=115200,
9-
parity=serial.PARITY_NONE,
9+
parity=serial.PARITY_ODD,
1010
stopbits=serial.STOPBITS_ONE,
1111
bytesize=serial.EIGHTBITS,
1212
rtscts=False)

utils/catkin_ws/src/joypad_bridge/scripts/velocities_pb2.py

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)