Skip to content

Commit dad0a85

Browse files
committed
cleanup motor_control
1 parent e31cc31 commit dad0a85

File tree

6 files changed

+352
-307
lines changed

6 files changed

+352
-307
lines changed

src/motor_control/dcmotor.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2023 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "dcmotor.h"
13+
14+
15+
DCmotor::DCmotor(const uint32_t _pinA, const uint32_t _chA, const uint32_t _pinB, const uint32_t _chB,
16+
const bool flip, TIM_TypeDef * _tim, const uint32_t _frequency){
17+
if (!flip){
18+
pinA=_pinA;
19+
chA=_chA;
20+
pinB=_pinB;
21+
chB=_chB;
22+
}
23+
else{
24+
pinA=_pinB;
25+
chA=_chB;
26+
pinB=_pinA;
27+
chB=_chA;
28+
}
29+
htimX.Instance = _tim;
30+
frequency=_frequency;
31+
pinMode(MOTORS_ENABLE,OUTPUT);
32+
}
33+
34+
void DCmotor::begin(){
35+
timX = new HardwareTimer(htimX.Instance);
36+
timX->setPWM(chA, pinA, frequency, 0);
37+
timX->setPWM(chB, pinB, frequency, 0);
38+
digitalWrite(MOTORS_ENABLE,HIGH);
39+
}
40+
41+
void DCmotor::disable(){
42+
digitalWrite(MOTORS_ENABLE,LOW);
43+
}
44+
45+
void DCmotor::setSpeed(const int speed){
46+
if (speed>=0){
47+
pwmWrite(chA,speed);
48+
pwmWrite(chB,0);
49+
}
50+
else{
51+
pwmWrite(chA,0);
52+
pwmWrite(chB,-speed);
53+
}
54+
}
55+
56+
void DCmotor::stop(){
57+
setSpeed(0);
58+
}

src/motor_control/dcmotor.h

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#define __DC_MOTOR_H__
1414

1515
#include "Arduino.h"
16+
#include "../definitions/pinout_definitions.h"
1617

1718
class DCmotor{
1819
private:
@@ -30,49 +31,15 @@ class DCmotor{
3031

3132
public:
3233
DCmotor(const uint32_t _pinA, const uint32_t _chA, const uint32_t _pinB, const uint32_t _chB,
33-
const bool flip=false, TIM_TypeDef * _tim = TIM2, const uint32_t _frequency=20000){
34-
if (!flip){
35-
pinA=_pinA;
36-
chA=_chA;
37-
pinB=_pinB;
38-
chB=_chB;
39-
}
40-
else{
41-
pinA=_pinB;
42-
chA=_chB;
43-
pinB=_pinA;
44-
chB=_chA;
45-
}
46-
htimX.Instance = _tim;
47-
frequency=_frequency;
48-
pinMode(MOTORS_ENABLE,OUTPUT);
49-
}
34+
const bool flip=false, TIM_TypeDef * _tim = TIM2, const uint32_t _frequency=20000);
5035

51-
void begin(){
52-
timX = new HardwareTimer(htimX.Instance);
53-
timX->setPWM(chA, pinA, frequency, 0);
54-
timX->setPWM(chB, pinB, frequency, 0);
55-
digitalWrite(MOTORS_ENABLE,HIGH);
56-
}
36+
void begin();
5737

58-
void disable(){
59-
digitalWrite(MOTORS_ENABLE,LOW);
60-
}
38+
void disable();
6139

62-
void setSpeed(const int speed){
63-
if (speed>=0){
64-
pwmWrite(chA,speed);
65-
pwmWrite(chB,0);
66-
}
67-
else{
68-
pwmWrite(chA,0);
69-
pwmWrite(chB,-speed);
70-
}
71-
}
40+
void setSpeed(const int speed);
7241

73-
void stop(){
74-
setSpeed(0);
75-
}
42+
void stop();
7643
};
7744

7845
#endif

src/motor_control/encoder.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
This file is part of the Arduino_AlvikCarrier library.
3+
4+
Copyright (c) 2023 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
10+
*/
11+
12+
#include "encoder.h"
13+
14+
Encoder::Encoder(TIM_TypeDef * _tim, bool _flip){
15+
memset(&htimX, 0, sizeof(htimX));
16+
htimX.Instance = _tim;
17+
flip = _flip;
18+
}
19+
20+
void Encoder::begin(){
21+
22+
//htimX.Instance= TIM2;
23+
htimX.Init.Prescaler=0;
24+
htimX.Init.CounterMode= TIM_COUNTERMODE_UP;
25+
htimX.Init.Period = 65535;
26+
htimX.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
27+
28+
29+
TIM_Encoder_InitTypeDef encoder_config;
30+
encoder_config.EncoderMode = TIM_ENCODERMODE_TI2;
31+
32+
if (flip){
33+
encoder_config.IC1Polarity=TIM_ICPOLARITY_FALLING;
34+
}
35+
else{
36+
encoder_config.IC1Polarity=TIM_ICPOLARITY_RISING;
37+
}
38+
39+
encoder_config.IC1Selection=TIM_ICSELECTION_DIRECTTI;
40+
encoder_config.IC1Prescaler=TIM_ICPSC_DIV1;
41+
encoder_config.IC1Filter=0;
42+
encoder_config.IC2Polarity=TIM_ICPOLARITY_RISING;
43+
encoder_config.IC2Selection=TIM_ICSELECTION_DIRECTTI;
44+
encoder_config.IC2Prescaler=TIM_ICPSC_DIV1;
45+
encoder_config.IC2Filter=0;
46+
47+
48+
HAL_TIM_Encoder_Init(&htimX, &encoder_config);
49+
HAL_TIM_Encoder_Start(&htimX, TIM_CHANNEL_ALL);
50+
}
51+
52+
int Encoder::getCount(){
53+
return int16_t(__HAL_TIM_GET_COUNTER(&htimX)); // it gives the sign
54+
}
55+
56+
void Encoder::reset(){
57+
htimX.Instance->CNT=0;
58+
}

src/motor_control/encoder.h

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,51 +19,13 @@ class Encoder{
1919
TIM_HandleTypeDef htimX;
2020
bool flip;
2121
public:
22-
Encoder(TIM_TypeDef * _tim, bool _flip=false){
23-
memset(&htimX, 0, sizeof(htimX));
24-
htimX.Instance = _tim;
25-
flip = _flip;
26-
}
22+
Encoder(TIM_TypeDef * _tim, bool _flip=false);
2723

28-
void begin(){
29-
30-
//htimX.Instance= TIM2;
31-
htimX.Init.Prescaler=0;
32-
htimX.Init.CounterMode= TIM_COUNTERMODE_UP;
33-
htimX.Init.Period = 65535;
34-
htimX.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
24+
void begin();
3525

26+
int getCount();
3627

37-
TIM_Encoder_InitTypeDef encoder_config;
38-
encoder_config.EncoderMode = TIM_ENCODERMODE_TI2;
39-
40-
if (flip){
41-
encoder_config.IC1Polarity=TIM_ICPOLARITY_FALLING;
42-
}
43-
else{
44-
encoder_config.IC1Polarity=TIM_ICPOLARITY_RISING;
45-
}
46-
47-
encoder_config.IC1Selection=TIM_ICSELECTION_DIRECTTI;
48-
encoder_config.IC1Prescaler=TIM_ICPSC_DIV1;
49-
encoder_config.IC1Filter=0;
50-
encoder_config.IC2Polarity=TIM_ICPOLARITY_RISING;
51-
encoder_config.IC2Selection=TIM_ICSELECTION_DIRECTTI;
52-
encoder_config.IC2Prescaler=TIM_ICPSC_DIV1;
53-
encoder_config.IC2Filter=0;
54-
55-
56-
HAL_TIM_Encoder_Init(&htimX, &encoder_config);
57-
HAL_TIM_Encoder_Start(&htimX, TIM_CHANNEL_ALL);
58-
}
59-
60-
int getCount(){
61-
return int16_t(__HAL_TIM_GET_COUNTER(&htimX)); // it gives the sign
62-
}
63-
64-
void reset(){
65-
htimX.Instance->CNT=0;
66-
}
28+
void reset();
6729

6830
};
6931

0 commit comments

Comments
 (0)