-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathapp.c
222 lines (180 loc) · 7.13 KB
/
app.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <stdio.h>
#include <unistd.h>
#include <inttypes.h>
#include <math.h>
#include <rcl/rcl.h>
#include <rcl/error_handling.h>
#include <rclc/rclc.h>
#include <rclc/executor.h>
#include <geometry_msgs/msg/twist.h>
#include <driver/gpio.h>
#include <driver/ledc.h>
#ifdef ESP_PLATFORM
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#endif
// Macro functions
#define constrain(amt, low, high) ((amt)<(low)?(low):((amt)>(high)?(high):(amt)))
#define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);vTaskDelete(NULL);}}
#define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}}
// Constants
#define FRAME_TIME 100 // 1000 / FRAME_TIME = FPS
#define SLEEP_TIME 10
// PINS
#define LED_BUILTIN 33
#define PIN_LEFT_FORWARD 12
#define PIN_LEFT_BACKWARD 13
#define PIN_RIGHT_FORWARD 15
#define PIN_RIGHT_BACKWARD 14
// PWM Channels (Reserve channel 0 and 1 for camera)
#define PWM_LEFT_FORWARD LEDC_CHANNEL_2
#define PWM_LEFT_BACKWARD LEDC_CHANNEL_3
#define PWM_RIGHT_FORWARD LEDC_CHANNEL_4
#define PWM_RIGHT_BACKWARD LEDC_CHANNEL_5
// Other PWM settings
#define PWM_FREQUENCY 50
#define PWM_RESOLUTION LEDC_TIMER_12_BIT
#define PWM_TIMER LEDC_TIMER_1
#define PWM_MODE LEDC_HIGH_SPEED_MODE
// These values are determined by experiment and are unique to every robot
#define PWM_MOTOR_MIN 750 // The value where the motor starts moving
#define PWM_MOTOR_MAX 4095 // Full speed (2^12 - 1)
geometry_msgs__msg__Twist msg;
// Function forward declarations
void setupPins();
void setupRos();
void cmd_vel_callback(const void *msgin);
void timer_callback(rcl_timer_t *timer, int64_t last_call_time);
float fmap(float val, float in_min, float in_max, float out_min, float out_max);
// Main
void appMain(void *arg) {
setupPins();
setupRos();
}
void setupPins() {
// Led. Set it to GPIO_MODE_INPUT_OUTPUT, because we want to read back the state we set it to.
gpio_reset_pin(LED_BUILTIN);
gpio_set_direction(LED_BUILTIN, GPIO_MODE_INPUT_OUTPUT);
// Configure timer
ledc_timer_config_t ledc_timer = {
.duty_resolution = PWM_RESOLUTION,
.freq_hz = PWM_FREQUENCY,
.speed_mode = PWM_MODE,
.timer_num = PWM_TIMER,
.clk_cfg = LEDC_AUTO_CLK,
};
ledc_timer_config(&ledc_timer);
// Configure 4 PWM channels and assign output pins
ledc_channel_config_t ledc_channel[4] = {
{
.channel = PWM_LEFT_FORWARD,
.duty = 0,
.gpio_num = PIN_LEFT_FORWARD,
.speed_mode = PWM_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_1
},
{
.channel = PWM_LEFT_BACKWARD,
.duty = 0,
.gpio_num = PIN_LEFT_BACKWARD,
.speed_mode = PWM_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_1
},
{
.channel = PWM_RIGHT_FORWARD,
.duty = 0,
.gpio_num = PIN_RIGHT_FORWARD,
.speed_mode = PWM_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_1
},
{
.channel = PWM_RIGHT_BACKWARD,
.duty = 0,
.gpio_num = PIN_RIGHT_BACKWARD,
.speed_mode = PWM_MODE,
.hpoint = 0,
.timer_sel = LEDC_TIMER_1
},
};
for (int i = 0; i < 4; i++) {
ledc_channel_config(&ledc_channel[i]);
}
}
void setupRos() {
// Micro ROS
rcl_allocator_t allocator = rcl_get_default_allocator();
rclc_support_t support;
// create init_options
RCCHECK(rclc_support_init(&support, 0, NULL, &allocator));
// create node
rcl_node_t node;
RCCHECK(rclc_node_init_default(&node, "ros_esp32cam_diffdrive", "", &support));
// create subscriber
rcl_subscription_t subscriber;
RCCHECK(rclc_subscription_init_default(
&subscriber,
&node,
ROSIDL_GET_MSG_TYPE_SUPPORT(geometry_msgs, msg, Twist),
"/cmd_vel"));
// create timer,
rcl_timer_t timer;
RCCHECK(rclc_timer_init_default(
&timer,
&support,
RCL_MS_TO_NS(FRAME_TIME),
timer_callback));
// create executor
rclc_executor_t executor;
RCCHECK(rclc_executor_init(&executor, &support.context, 2, &allocator));
RCCHECK(rclc_executor_add_subscription(&executor, &subscriber, &msg, &cmd_vel_callback, ON_NEW_DATA));
RCCHECK(rclc_executor_add_timer(&executor, &timer));
while (1) {
rclc_executor_spin_some(&executor, RCL_MS_TO_NS(SLEEP_TIME));
usleep(SLEEP_TIME * 1000);
}
// free resources
RCCHECK(rcl_subscription_fini(&subscriber, &node));
RCCHECK(rcl_node_fini(&node));
vTaskDelete(NULL);
}
// We don't really need the callback, because msg is set anyway
void cmd_vel_callback(const void *msgin) {
// const geometry_msgs__msg__Twist *msg = (const geometry_msgs__msg__Twist *) msgin;
// printf("Message received: %f %f\n", msg->linear.x, msg->angular.z);
}
// Each frame, check msg data and set PWM channels accordingly
void timer_callback(rcl_timer_t *timer, int64_t last_call_time) {
gpio_set_level(LED_BUILTIN, !gpio_get_level(LED_BUILTIN));
if (timer == NULL) {
return;
}
// Use linear.x for forward value and angular.z for rotation
float linear = constrain(msg.linear.x, -1, 1);
float angular = constrain(msg.angular.z, -1, 1);
// This robot is an RC tank and uses a differential drive (skid steering).
// Calculate the speed of left and right motors. Simple version without wheel distances.
// Taken from https://hackernoon.com/unicycle-to-differential-drive-courseras-control-of-mobile-robots-with-ros-and-rosbots-part-2-6d27d15f2010#1e59
float left = (linear - angular) / 2.0f;
float right = (linear + angular) / 2.0f;
// Then map those values to PWM intensities. PWM_MOTOR_MAX = full speed, PWM_MOTOR_MIN = the minimal amount of power at which the motors begin moving.
uint16_t pwmLeft = (uint16_t) fmap(fabs(left), 0, 1, PWM_MOTOR_MIN, PWM_MOTOR_MAX);
uint16_t pwmRight = (uint16_t) fmap(fabs(right), 0, 1, PWM_MOTOR_MIN, PWM_MOTOR_MAX);
// Each wheel has a channel for forwards and backwards movement
ledc_set_duty(PWM_MODE, PWM_LEFT_FORWARD, pwmLeft * (left > 0));
ledc_set_duty(PWM_MODE, PWM_LEFT_BACKWARD, pwmLeft * (left < 0));
ledc_set_duty(PWM_MODE, PWM_RIGHT_FORWARD, pwmRight * (right > 0));
ledc_set_duty(PWM_MODE, PWM_RIGHT_BACKWARD, pwmRight * (right < 0));
ledc_update_duty(PWM_MODE, PWM_LEFT_FORWARD);
ledc_update_duty(PWM_MODE, PWM_LEFT_BACKWARD);
ledc_update_duty(PWM_MODE, PWM_RIGHT_FORWARD);
ledc_update_duty(PWM_MODE, PWM_RIGHT_BACKWARD);
printf("%d, %d %d, %d, %d %d, %f, %f\n", pwmLeft, left > 0, left < 0, pwmRight, right > 0, right < 0, left, right);
}
// Helper functions
// -------------------------------------------------------------------
float fmap(float val, float in_min, float in_max, float out_min, float out_max) {
return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}