Skip to content

Commit 5124940

Browse files
authored
Merge pull request #1 from gbr1/dev
0.0.1
2 parents 3de9e0a + c2835b4 commit 5124940

File tree

15 files changed

+844
-0
lines changed

15 files changed

+844
-0
lines changed

Arduino_Robot_Firmware.cpp

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "Arduino_Robot_Firmware.h"
2+
3+
4+
Arduino_Robot_Firmware::Arduino_Robot_Firmware(){
5+
// I2C internal bus
6+
wire = new TwoWire(I2C_1_SDA, I2C_1_SCL);
7+
8+
// RGB leds
9+
led1 = new RGBled(LED_1_RED,LED_1_GREEN,LED_1_BLUE);
10+
led2 = new RGBled(LED_2_RED,LED_2_GREEN,LED_2_BLUE);
11+
12+
// motors
13+
motor_left = new DCmotor(MOTOR_LEFT_A,MOTOR_LEFT_A_CH, MOTOR_LEFT_B, MOTOR_LEFT_B_CH,true);
14+
motor_right = new DCmotor(MOTOR_RIGHT_A,MOTOR_RIGHT_A_CH,MOTOR_RIGHT_B,MOTOR_RIGHT_B_CH);
15+
16+
// encoders
17+
encoder_left = new Encoder(TIM3);
18+
encoder_right = new Encoder(TIM5);
19+
20+
// color sensor
21+
apds9960 = new APDS9960(*wire,APDS_INT);
22+
}
23+
24+
int Arduino_Robot_Firmware::begin(){
25+
// setup alternate functions
26+
AF_Tim2_pwm();
27+
AF_Tim5_pins_encoder();
28+
AF_Tim3_pins_encoder();
29+
30+
// turn off leds
31+
led1->clear();
32+
led2->clear();
33+
34+
motor_left->begin();
35+
motor_right->begin();
36+
motor_left->stop();
37+
motor_right->stop();
38+
39+
encoder_left->begin();
40+
encoder_right->begin();
41+
42+
wire->begin();
43+
44+
beginAPDS();
45+
46+
return 0;
47+
}
48+
49+
50+
/******************************************************************************************************/
51+
/* Color sensor, APDS9960 */
52+
/******************************************************************************************************/
53+
54+
int Arduino_Robot_Firmware::beginAPDS(){
55+
pinMode(APDS_LED,OUTPUT);
56+
enableIlluminator();
57+
apds9960->begin();
58+
return 0;
59+
}
60+
61+
void Arduino_Robot_Firmware::updateAPDS(){
62+
if (apds9960->proximityAvailable()){
63+
bottom_proximity=apds9960->readProximity();
64+
}
65+
//digitalWrite(APDS_LED,HIGH);
66+
if (apds9960->colorAvailable()){
67+
apds9960->readColor(bottom_red,bottom_green,bottom_blue,bottom_clear);
68+
}
69+
//digitalWrite(APDS_LED,LOW);
70+
}
71+
72+
void Arduino_Robot_Firmware::setIlluminator(uint8_t value){
73+
digitalWrite(APDS_LED,value);
74+
}
75+
76+
void Arduino_Robot_Firmware::enableIlluminator(){
77+
setIlluminator(HIGH);
78+
}
79+
80+
void Arduino_Robot_Firmware::disableIlluminator(){
81+
setIlluminator(LOW);
82+
}
83+
84+
int Arduino_Robot_Firmware::getRed(){
85+
return bottom_red;
86+
}
87+
88+
int Arduino_Robot_Firmware::getGreen(){
89+
return bottom_green;
90+
}
91+
92+
int Arduino_Robot_Firmware::getBlue(){
93+
return bottom_blue;
94+
}
95+
96+
int Arduino_Robot_Firmware::getProximity(){
97+
return bottom_proximity;
98+
}

Arduino_Robot_Firmware.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef __ARDUINO_ROBOT_FIRMWARE_H__
2+
#define __ARDUINO_ROBOT_FIRMWARE_H__
3+
4+
#include "Arduino.h"
5+
#include "pinout_definitions.h"
6+
#include "HAL_custom_init.h"
7+
#include "encoder.h"
8+
#include "rgb_led.h"
9+
#include "dcmotor.h"
10+
#include "motor_control.h"
11+
#include "Arduino_APDS9960.h"
12+
13+
class Arduino_Robot_Firmware{
14+
private:
15+
APDS9960 * apds9960;
16+
int bottom_red, bottom_green, bottom_blue, bottom_clear, bottom_proximity;
17+
18+
public:
19+
RGBled * led1;
20+
RGBled * led2;
21+
DCmotor * motor_left;
22+
DCmotor * motor_right;
23+
Encoder * encoder_left;
24+
Encoder * encoder_right;
25+
TwoWire * wire;
26+
27+
28+
Arduino_Robot_Firmware();
29+
30+
int begin();
31+
32+
33+
34+
// Color sensor, APDS9960
35+
int beginAPDS(); // initialize all components required by color detection
36+
void updateAPDS(); // refresh data
37+
void setIlluminator(uint8_t value); // set white leds
38+
void enableIlluminator(); // white leds on
39+
void disableIlluminator(); // white leds off
40+
int getRed(); // red value 0-255
41+
int getGreen(); // green value 0-255
42+
int getBlue(); // blue value 0-255
43+
int getProximity(); // proximity value 0-127
44+
};
45+
46+
#endif

HAL_custom_init.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#ifndef __HAL_CUSTOM_INIT_H__
2+
#define __HAL_CUSTOM_INIT_H__
3+
4+
5+
// Set PA_0 and PA_1 as alternate function 1 -> TIM2
6+
/*
7+
void AF_Tim2_pins_encoder(){
8+
GPIO_InitTypeDef GPIO_InitStruct;
9+
__HAL_RCC_TIM2_CLK_ENABLE();
10+
__HAL_RCC_GPIOA_CLK_ENABLE();
11+
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
12+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
13+
GPIO_InitStruct.Pull = GPIO_NOPULL;
14+
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
15+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
16+
}
17+
*/
18+
19+
20+
/***************************************************************/
21+
/* Encoder */
22+
/***************************************************************/
23+
24+
25+
// Set PA_0 and PA_1 as alternate function 1 -> TIM5
26+
void AF_Tim5_pins_encoder(){
27+
GPIO_InitTypeDef GPIO_InitStruct;
28+
__HAL_RCC_TIM5_CLK_ENABLE();
29+
__HAL_RCC_GPIOA_CLK_ENABLE();
30+
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
31+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
32+
GPIO_InitStruct.Pull = GPIO_NOPULL;
33+
GPIO_InitStruct.Alternate = GPIO_AF2_TIM5;
34+
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
35+
}
36+
37+
// Set PC_6 and PC_7 as alternate function 2 -> TIM3
38+
void AF_Tim3_pins_encoder(){
39+
GPIO_InitTypeDef GPIO_InitStruct;
40+
__HAL_RCC_TIM3_CLK_ENABLE();
41+
__HAL_RCC_GPIOC_CLK_ENABLE();
42+
GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
43+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
44+
GPIO_InitStruct.Pull = GPIO_NOPULL;
45+
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
46+
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
47+
}
48+
49+
/***************************************************************/
50+
/* PWM */
51+
/***************************************************************/
52+
53+
void AF_Tim2_pwm(){
54+
GPIO_InitTypeDef GPIO_InitStructA;
55+
__HAL_RCC_TIM2_CLK_ENABLE();
56+
__HAL_RCC_GPIOA_CLK_ENABLE();
57+
GPIO_InitStructA.Pin = GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_15;
58+
GPIO_InitStructA.Mode = GPIO_MODE_AF_PP;
59+
GPIO_InitStructA.Pull = GPIO_NOPULL;
60+
GPIO_InitStructA.Alternate = GPIO_AF1_TIM2;
61+
HAL_GPIO_Init(GPIOA, &GPIO_InitStructA);
62+
63+
64+
GPIO_InitTypeDef GPIO_InitStructB;
65+
__HAL_RCC_TIM2_CLK_ENABLE();
66+
__HAL_RCC_GPIOB_CLK_ENABLE();
67+
GPIO_InitStructB.Pin = GPIO_PIN_3;
68+
GPIO_InitStructB.Mode = GPIO_MODE_AF_PP;
69+
GPIO_InitStructB.Pull = GPIO_NOPULL;
70+
GPIO_InitStructB.Alternate = GPIO_AF1_TIM2;
71+
HAL_GPIO_Init(GPIOB, &GPIO_InitStructB);
72+
}
73+
74+
75+
76+
77+
#endif

dcmotor.h

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#ifndef __DC_MOTOR_H__
2+
#define __DC_MOTOR_H__
3+
4+
#include "Arduino.h"
5+
6+
class DCmotor{
7+
private:
8+
uint32_t pinA;
9+
uint32_t chA;
10+
uint32_t pinB;
11+
uint32_t chB;
12+
TIM_HandleTypeDef htimX;
13+
HardwareTimer * timX;
14+
uint32_t frequency;
15+
16+
void pwmWrite(uint32_t ch, uint32_t value){
17+
timX->setCaptureCompare(ch, value, RESOLUTION_12B_COMPARE_FORMAT);
18+
}
19+
20+
public:
21+
DCmotor(const uint32_t _pinA, const uint32_t _chA, const uint32_t _pinB, const uint32_t _chB,
22+
const bool flip=false, TIM_TypeDef * _tim = TIM2, const uint32_t _frequency=20000){
23+
if (!flip){
24+
pinA=_pinA;
25+
chA=_chA;
26+
pinB=_pinB;
27+
chB=_chB;
28+
}
29+
else{
30+
pinA=_pinB;
31+
chA=_chB;
32+
pinB=_pinA;
33+
chB=_chA;
34+
}
35+
htimX.Instance = _tim;
36+
frequency=_frequency;
37+
pinMode(MOTORS_ENABLE,OUTPUT);
38+
}
39+
40+
void begin(){
41+
timX = new HardwareTimer(htimX.Instance);
42+
timX->setPWM(chA, pinA, frequency, 0);
43+
timX->setPWM(chB, pinB, frequency, 0);
44+
digitalWrite(MOTORS_ENABLE,HIGH);
45+
}
46+
47+
void disable(){
48+
digitalWrite(MOTORS_ENABLE,LOW);
49+
}
50+
51+
void setSpeed(const int speed){
52+
if (speed>=0){
53+
pwmWrite(chA,speed);
54+
pwmWrite(chB,0);
55+
}
56+
else{
57+
pwmWrite(chA,0);
58+
pwmWrite(chB,-speed);
59+
}
60+
}
61+
62+
void stop(){
63+
setSpeed(0);
64+
}
65+
};
66+
67+
#endif

encoder.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#ifndef __ENCODER_H__
2+
#define __ENCODER_H__
3+
4+
#include "Arduino.h"
5+
6+
class Encoder{
7+
private:
8+
TIM_HandleTypeDef htimX;
9+
bool flip;
10+
public:
11+
Encoder(TIM_TypeDef * _tim, bool _flip=false){
12+
htimX.Instance = _tim;
13+
flip = _flip;
14+
}
15+
16+
void begin(){
17+
18+
//htimX.Instance= TIM2;
19+
htimX.Init.Prescaler=0;
20+
htimX.Init.CounterMode= TIM_COUNTERMODE_UP;
21+
htimX.Init.Period = 65535;
22+
htimX.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
23+
24+
25+
TIM_Encoder_InitTypeDef encoder_config;
26+
encoder_config.EncoderMode = TIM_ENCODERMODE_TI2;
27+
28+
if (flip){
29+
encoder_config.IC1Polarity=TIM_ICPOLARITY_FALLING;
30+
}
31+
else{
32+
encoder_config.IC1Polarity=TIM_ICPOLARITY_RISING;
33+
}
34+
35+
encoder_config.IC1Selection=TIM_ICSELECTION_DIRECTTI;
36+
encoder_config.IC1Prescaler=TIM_ICPSC_DIV1;
37+
encoder_config.IC1Filter=0;
38+
encoder_config.IC2Polarity=TIM_ICPOLARITY_RISING;
39+
encoder_config.IC2Selection=TIM_ICSELECTION_DIRECTTI;
40+
encoder_config.IC2Prescaler=TIM_ICPSC_DIV1;
41+
encoder_config.IC2Filter=0;
42+
43+
44+
HAL_TIM_Encoder_Init(&htimX, &encoder_config);
45+
HAL_TIM_Encoder_Start(&htimX, TIM_CHANNEL_ALL);
46+
}
47+
48+
int getCount(){
49+
return int16_t(__HAL_TIM_GET_COUNTER(&htimX)); // it gives the sign
50+
}
51+
52+
void reset(){
53+
htimX.Instance->CNT=0;
54+
}
55+
56+
};
57+
58+
#endif

examples/.DS_Store

6 KB
Binary file not shown.

0 commit comments

Comments
 (0)