Skip to content

Commit 8a95745

Browse files
authored
Merge pull request linorobot#79 from hippo5329/pr-esp32
Add esp32 support
2 parents 09ac777 + 1ba8117 commit 8a95745

16 files changed

+373
-9225
lines changed

config/config.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
#ifndef CONFIG_H
15+
#define CONFIG_H
1416

1517
#ifdef USE_VATTENKAR_CONFIG
1618
#include "custom/vattenkar_config.h"
@@ -20,6 +22,13 @@
2022
#include "custom/dev_config.h"
2123
#endif
2224

23-
#if !defined (USE_VATTENKAR_CONFIG)
25+
#ifdef USE_ESP32_CONFIG
26+
#include "custom/esp32_config.h"
27+
#endif
28+
29+
// this should be the last one
30+
#ifndef LINO_BASE
2431
#include "lino_base_config.h"
25-
#endif
32+
#endif
33+
34+
#endif

config/custom/esp32_config.h

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
// Copyright (c) 2021 Juan Miguel Jimeno
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.esp32
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#ifndef ESP32_CONFIG_H
16+
#define ESP32_CONFIG_H
17+
18+
#define LED_PIN 2 //used for debugging status
19+
20+
//uncomment the base you're building
21+
#define LINO_BASE DIFFERENTIAL_DRIVE // 2WD and Tracked robot w/ 2 motors
22+
// #define LINO_BASE SKID_STEER // 4WD robot
23+
// #define LINO_BASE MECANUM // Mecanum drive robot
24+
25+
//uncomment the motor driver you're using
26+
// #define USE_GENERIC_2_IN_MOTOR_DRIVER // Motor drivers with 2 Direction Pins(INA, INB) and 1 PWM(ENABLE) pin ie. L298, L293, VNH5019
27+
// #define USE_GENERIC_1_IN_MOTOR_DRIVER // Motor drivers with 1 Direction Pin(INA) and 1 PWM(ENABLE) pin.
28+
#define USE_BTS7960_MOTOR_DRIVER // BTS7970 Motor Driver using A4950 (<40V) module or DRV8833 (<10V)
29+
// #define USE_ESC_MOTOR_DRIVER // Motor ESC for brushless motors
30+
31+
//uncomment the IMU you're using
32+
// #define USE_GY85_IMU
33+
#define USE_MPU6050_IMU
34+
// #define USE_MPU9150_IMU
35+
// #define USE_MPU9250_IMU
36+
// #define USE_QMI8658_IMU
37+
// #define USE_HMC5883L_MAG
38+
// #define USE_AK8963_MAG
39+
// #define USE_AK8975_MAG
40+
// #define USE_AK09918_MAG
41+
// #define USE_QMC5883L_MAG
42+
// #define MAG_BIAS { 0, 0, 0 }
43+
44+
#define K_P 0.6 // P constant
45+
#define K_I 0.8 // I constant
46+
#define K_D 0.5 // D constant
47+
48+
/*
49+
ROBOT ORIENTATION
50+
FRONT
51+
MOTOR1 MOTOR2 (2WD/ACKERMANN)
52+
MOTOR3 MOTOR4 (4WD/MECANUM)
53+
BACK
54+
*/
55+
56+
//define your robot' specs here
57+
#define MOTOR_MAX_RPM 150 // motor's max RPM
58+
#define MAX_RPM_RATIO 0.85 // max RPM allowed for each MAX_RPM_ALLOWED = MOTOR_MAX_RPM * MAX_RPM_RATIO
59+
#define MOTOR_OPERATING_VOLTAGE 12 // motor's operating voltage (used to calculate max RPM)
60+
#define MOTOR_POWER_MAX_VOLTAGE 12 // max voltage of the motor's power source (used to calculate max RPM)
61+
#define MOTOR_POWER_MEASURED_VOLTAGE 12 // current voltage reading of the power connected to the motor (used for calibration)
62+
#define COUNTS_PER_REV1 550 // wheel1 encoder's no of ticks per rev
63+
#define COUNTS_PER_REV2 550 // wheel2 encoder's no of ticks per rev
64+
#define COUNTS_PER_REV3 550 // wheel3 encoder's no of ticks per rev
65+
#define COUNTS_PER_REV4 550 // wheel4 encoder's no of ticks per rev
66+
#define WHEEL_DIAMETER 0.0560 // wheel's diameter in meters
67+
#define LR_WHEELS_DISTANCE 0.224 // distance between left and right wheels
68+
#define PWM_BITS 10 // PWM Resolution of the microcontroller
69+
#define PWM_FREQUENCY 20000 // PWM Frequency
70+
71+
// INVERT ENCODER COUNTS
72+
#define MOTOR1_ENCODER_INV false
73+
#define MOTOR2_ENCODER_INV false
74+
#define MOTOR3_ENCODER_INV false
75+
#define MOTOR4_ENCODER_INV false
76+
77+
// INVERT MOTOR DIRECTIONS
78+
#define MOTOR1_INV false
79+
#define MOTOR2_INV false
80+
#define MOTOR3_INV false
81+
#define MOTOR4_INV false
82+
83+
// ENCODER PINS
84+
#define MOTOR1_ENCODER_A 36
85+
#define MOTOR1_ENCODER_B 39
86+
87+
#define MOTOR2_ENCODER_A 35
88+
#define MOTOR2_ENCODER_B 34
89+
90+
#define MOTOR3_ENCODER_A 32
91+
#define MOTOR3_ENCODER_B 27
92+
93+
#define MOTOR4_ENCODER_A 26
94+
#define MOTOR4_ENCODER_B 25
95+
96+
// MOTOR PINS
97+
#ifdef USE_GENERIC_2_IN_MOTOR_DRIVER
98+
#define MOTOR1_PWM 21 //Pin no 21 is not a PWM pin on Teensy 4.x, you can swap it with pin no 1 instead.
99+
#define MOTOR1_IN_A 20
100+
#define MOTOR1_IN_B 1
101+
102+
#define MOTOR2_PWM 5
103+
#define MOTOR2_IN_A 6
104+
#define MOTOR2_IN_B 8
105+
106+
#define MOTOR3_PWM 22
107+
#define MOTOR3_IN_A 23
108+
#define MOTOR3_IN_B 0
109+
110+
#define MOTOR4_PWM 4
111+
#define MOTOR4_IN_A 3
112+
#define MOTOR4_IN_B 2
113+
114+
#define PWM_MAX pow(2, PWM_BITS) - 1
115+
#define PWM_MIN -PWM_MAX
116+
#endif
117+
118+
#ifdef USE_GENERIC_1_IN_MOTOR_DRIVER
119+
#define MOTOR1_PWM 21 //Pin no 21 is not a PWM pin on Teensy 4.x, you can use pin no 1 instead.
120+
#define MOTOR1_IN_A 20
121+
#define MOTOR1_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
122+
123+
#define MOTOR2_PWM 5
124+
#define MOTOR2_IN_A 6
125+
#define MOTOR2_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
126+
127+
#define MOTOR3_PWM 22
128+
#define MOTOR3_IN_A 23
129+
#define MOTOR3_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
130+
131+
#define MOTOR4_PWM 4
132+
#define MOTOR4_IN_A 3
133+
#define MOTOR4_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
134+
135+
#define PWM_MAX pow(2, PWM_BITS) - 1
136+
#define PWM_MIN -PWM_MAX
137+
#endif
138+
139+
#ifdef USE_BTS7960_MOTOR_DRIVER
140+
#define MOTOR1_PWM -1 //DON'T TOUCH THIS! This is just a placeholder
141+
#define MOTOR1_IN_A 19 // Pin no 21 is not a PWM pin on Teensy 4.x, you can use pin no 1 instead.
142+
#define MOTOR1_IN_B 18 // Pin no 20 is not a PWM pin on Teensy 4.x, you can use pin no 0 instead.
143+
144+
#define MOTOR2_PWM -1 //DON'T TOUCH THIS! This is just a placeholder
145+
#define MOTOR2_IN_A 16
146+
#define MOTOR2_IN_B 17
147+
148+
#define MOTOR3_PWM -1 //DON'T TOUCH THIS! This is just a placeholder
149+
#define MOTOR3_IN_A 13
150+
#define MOTOR3_IN_B 12
151+
152+
#define MOTOR4_PWM -1 //DON'T TOUCH THIS! This is just a placeholder
153+
#define MOTOR4_IN_A 4
154+
#define MOTOR4_IN_B 23
155+
156+
#define PWM_MAX pow(2, PWM_BITS) - 1
157+
#define PWM_MIN -PWM_MAX
158+
#endif
159+
160+
#ifdef USE_ESC_MOTOR_DRIVER
161+
#define MOTOR1_PWM 21 //Pin no 21 is not a PWM pin on Teensy 4.x. You can use pin no 1 instead.
162+
#define MOTOR1_IN_A -1 //DON'T TOUCH THIS! This is just a placeholder
163+
#define MOTOR1_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
164+
165+
#define MOTOR2_PWM 5
166+
#define MOTOR2_IN_A -1 //DON'T TOUCH THIS! This is just a placeholder
167+
#define MOTOR2_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
168+
169+
#define MOTOR3_PWM 22
170+
#define MOTOR3_IN_A -1 //DON'T TOUCH THIS! This is just a placeholder
171+
#define MOTOR3_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
172+
173+
#define MOTOR4_PWM 4
174+
#define MOTOR4_IN_A -1 //DON'T TOUCH THIS! This is just a placeholder
175+
#define MOTOR4_IN_B -1 //DON'T TOUCH THIS! This is just a placeholder
176+
177+
#define PWM_MAX 400
178+
#define PWM_MIN -PWM_MAX
179+
#endif
180+
181+
// #define USE_WIFI_TRANSPORT // use micro ros wifi transport
182+
#define AGENT_IP { 192, 168, 1, 100 } // eg IP of the desktop computer
183+
#define AGENT_PORT 8888
184+
// Enable WiFi with null terminated list of multiple APs SSID and password
185+
// #define WIFI_AP_LIST {{"WIFI_SSID", "WIFI_PASSWORD"}, {NULL}}
186+
#define WIFI_MONITOR 2 // min. period to send wifi signal strength to syslog
187+
// #define USE_ARDUINO_OTA
188+
// #define USE_SYSLOG
189+
#define SYSLOG_SERVER { 192, 168, 1, 100 } // eg IP of the desktop computer
190+
#define SYSLOG_PORT 514
191+
#define DEVICE_HOSTNAME "esp32"
192+
#define APP_NAME "hardware"
193+
// #define USE_LIDAR_UDP
194+
#define LIDAR_RXD 14
195+
// #define LIDAR_PWM 15
196+
#define LIDAR_SERIAL 1 // uart number
197+
#define LIDAR_BAUDRATE 230400
198+
#define LIDAR_SERVER { 192, 168, 1, 100 } // eg IP of the desktop computer
199+
#define LIDAR_PORT 8889
200+
#define BAUDRATE 115200
201+
// #define SDA_PIN 21 // specify I2C pins
202+
// #define SCL_PIN 22
203+
#define NODE_NAME "esp32"
204+
// #define TOPIC_PREFIX "esp32/"
205+
206+
// battery voltage ADC pin
207+
#define BATTERY_PIN 33
208+
// 3.3V ref, 12 bits ADC, 33k + 10k voltage divider
209+
// #define USE_ADC_LUT
210+
#ifdef USE_ADC_LUT
211+
const int16_t ADC_LUT[4096] = { /* insert adc_calibrate data here */ };
212+
#define BATTERY_ADJUST(v) (ADC_LUT[v] * (3.3 / 4096 * (33 + 10) / 10 * 1.0))
213+
#else
214+
#define BATTERY_ADJUST(v) ((v) * (3.3 / 4096 * (33 + 10) / 10))
215+
#endif
216+
// #define USE_INA219
217+
// #define TRIG_PIN 31 // ultrasonic sensor HC-SR04
218+
// #define ECHO_PIN 32
219+
#define USE_SHORT_BRAKE // for shorter stopping distance
220+
// #define WDT_TIMEOUT 60 // Sec
221+
// #define BOARD_INIT sleep(5) // wait to begin IMU calibration
222+
223+
#ifdef USE_SYSLOG
224+
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){ \
225+
syslog(LOG_ERR, "%s RCCHECK failed %d", __FUNCTION__, temp_rc); \
226+
return false; }}
227+
#else
228+
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){ \
229+
flashLED(3); \
230+
return false; }} // do not block
231+
#endif
232+
233+
#endif

firmware/lib/encoder/encoder.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11

22
#include "encoder.h"
33

4+
#if !defined(ESP32)
45
// Yes, all the code is in the header file, to provide the user
56
// configure options with #define (before they include it), and
67
// to facilitate some crafty optimizations!
78

89
Encoder_internal_state_t * Encoder::interruptArgs[];
9-
10+
#endif
1011

firmware/lib/encoder/encoder.h

+52
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,57 @@
2929
#ifndef Encoder_h_
3030
#define Encoder_h_
3131

32+
#ifdef ESP32
33+
#include "Arduino.h"
34+
#include <ESP32Encoder.h>
35+
class Encoder
36+
{
37+
private:
38+
int counts_per_rev_ = -1;
39+
ESP32Encoder encoder_;
40+
unsigned long prev_update_time_;
41+
int64_t prev_encoder_ticks_;
42+
public:
43+
Encoder(int pin1, int pin2, int counts_per_rev, bool invert = false) {
44+
int temp_pin = pin1;
45+
if (pin1 < 0 || pin2 < 0) return; // unused encoder
46+
if(invert)
47+
{
48+
pin1 = pin2;
49+
pin2 = temp_pin;
50+
}
51+
counts_per_rev_ = counts_per_rev;
52+
ESP32Encoder::useInternalWeakPullResistors = UP;
53+
encoder_.attachHalfQuad(pin1, pin2);
54+
}
55+
float getRPM() {
56+
if (counts_per_rev_ < 0) return 0.0;
57+
int64_t encoder_ticks = encoder_.getCount();
58+
//this function calculates the motor's RPM based on encoder ticks and delta time
59+
unsigned long current_time = micros();
60+
unsigned long dt = current_time - prev_update_time_;
61+
62+
//convert the time from milliseconds to minutes
63+
double dtm = (double)dt / 60000000;
64+
int64_t delta_ticks = encoder_ticks - prev_encoder_ticks_;
65+
66+
//calculate wheel's speed (RPM)
67+
prev_update_time_ = current_time;
68+
prev_encoder_ticks_ = encoder_ticks;
69+
70+
return (((double) delta_ticks / counts_per_rev_) / dtm);
71+
}
72+
inline int32_t read() {
73+
if (counts_per_rev_ < 0) return 0;
74+
return encoder_.getCount();
75+
}
76+
inline void write(int32_t p) {
77+
if (counts_per_rev_ < 0) return;
78+
encoder_.setCount(p);
79+
}
80+
};
81+
#else
82+
3283
#if defined(ARDUINO) && ARDUINO >= 100
3384
#include "Arduino.h"
3485
#elif defined(WIRING)
@@ -1001,5 +1052,6 @@ ISR(INT7_vect) { Encoder::update(Encoder::interruptArgs[SCRAMBLE_INT_ORDER(7)]);
10011052
#endif
10021053
#endif // ENCODER_OPTIMIZE_INTERRUPTS
10031054

1055+
#endif // ESP32
10041056

10051057
#endif

0 commit comments

Comments
 (0)