Skip to content

Commit 4993732

Browse files
committed
Refactor code structure for improved readability and maintainability
1 parent 5952e74 commit 4993732

23 files changed

Lines changed: 1959 additions & 0 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 1996-2022 Cyberbotics Ltd.
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.
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+
# Webots Makefile system
16+
#
17+
# You may add some variable definitions hereafter to customize the build process
18+
# See documentation in $(WEBOTS_HOME_PATH)/resources/Makefile.include
19+
20+
21+
# Do not modify the following: this includes Webots global Makefile.include
22+
null :=
23+
space := $(null) $(null)
24+
WEBOTS_HOME_PATH?=$(subst $(space),\ ,$(strip $(subst \,/,$(WEBOTS_HOME))))
25+
include $(WEBOTS_HOME_PATH)/resources/Makefile.include
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
/*
2+
* Copyright 1996-2022 Cyberbotics Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* Description: Boomer tractor example
19+
*/
20+
21+
#include <stdio.h>
22+
#include <string.h>
23+
#include <webots/camera.h>
24+
#include <webots/gps.h>
25+
#include <webots/keyboard.h>
26+
#include <webots/led.h>
27+
#include <webots/lidar.h>
28+
#include <webots/motor.h>
29+
#include <webots/robot.h>
30+
31+
// to be used as array indices
32+
enum { X, Y, Z };
33+
34+
// This needs to be changed if the .wbt model changes
35+
#define FRONT_WHEEL_RADIUS 0.38
36+
#define REAR_WHEEL_RADIUS 0.6
37+
38+
// devices
39+
WbDeviceTag left_steer, right_steer;
40+
WbDeviceTag left_front_wheel, right_front_wheel;
41+
WbDeviceTag left_rear_wheel, right_rear_wheel;
42+
43+
// lights
44+
WbDeviceTag left_flasher, right_flasher, tail_lights;
45+
WbDeviceTag work_head_lights, road_head_lights;
46+
47+
// camera
48+
WbDeviceTag camera;
49+
int camera_width = -1;
50+
int camera_height = -1;
51+
double camera_fov = -1.0;
52+
53+
// SICK laser
54+
WbDeviceTag sick;
55+
int sick_width = -1;
56+
double sick_range = -1.0;
57+
double sick_fov = -1.0;
58+
59+
// GPS
60+
WbDeviceTag gps;
61+
double gps_coords[3];
62+
double gps_speed = 0.0;
63+
64+
// misc variables
65+
int time_step = -1;
66+
double speed = 0.0;
67+
double steering_angle = 0.0;
68+
double manual_steering = 0.0;
69+
70+
void blink_lights() {
71+
int on = (int)wb_robot_get_time() % 2;
72+
wb_led_set(left_flasher, on);
73+
wb_led_set(right_flasher, on);
74+
wb_led_set(tail_lights, on);
75+
wb_led_set(work_head_lights, on);
76+
wb_led_set(road_head_lights, on);
77+
}
78+
79+
void print_help() {
80+
printf("You can drive this vehicle!\n");
81+
printf("Select the 3D window and then use the cursor keys to:\n");
82+
printf("[LEFT]/[RIGHT] - steer\n");
83+
printf("[UP]/[DOWN] - accelerate/slow down\n");
84+
}
85+
86+
// set target speed
87+
void set_speed(double kmh) {
88+
if (kmh > 30.0)
89+
kmh = 30.0;
90+
91+
speed = kmh;
92+
93+
printf("setting speed to %g km/h\n", kmh);
94+
95+
double front_ang_vel = kmh * 1000.0 / 3600.0 / FRONT_WHEEL_RADIUS;
96+
double rear_ang_vel = kmh * 1000.0 / 3600.0 / REAR_WHEEL_RADIUS;
97+
98+
// set motor rotation speed
99+
wb_motor_set_velocity(left_front_wheel, front_ang_vel);
100+
wb_motor_set_velocity(right_front_wheel, front_ang_vel);
101+
wb_motor_set_velocity(left_rear_wheel, rear_ang_vel);
102+
wb_motor_set_velocity(right_rear_wheel, rear_ang_vel);
103+
}
104+
105+
// positive: turn right, negative: turn left
106+
void set_steering_angle(double wheel_angle) {
107+
steering_angle = wheel_angle;
108+
wb_motor_set_position(left_steer, steering_angle);
109+
wb_motor_set_position(right_steer, steering_angle);
110+
}
111+
112+
void change_manual_steer_angle(double inc) {
113+
double new_manual_steering = manual_steering + inc;
114+
printf("steer %f\n", new_manual_steering);
115+
if (new_manual_steering <= 0.94 && new_manual_steering >= -0.94) {
116+
manual_steering = new_manual_steering;
117+
set_steering_angle(manual_steering);
118+
}
119+
120+
if (manual_steering == 0)
121+
printf("going straight\n");
122+
else
123+
printf("turning %.2f rad (%s)\n", steering_angle, steering_angle < 0 ? "left" : "right");
124+
}
125+
126+
void check_keyboard() {
127+
int key = wb_keyboard_get_key();
128+
switch (key) {
129+
case WB_KEYBOARD_UP:
130+
set_speed(speed + 1.0);
131+
break;
132+
case WB_KEYBOARD_DOWN:
133+
set_speed(speed - 1.0);
134+
break;
135+
case ' ':
136+
set_speed(0.0);
137+
break;
138+
case WB_KEYBOARD_RIGHT:
139+
change_manual_steer_angle(+0.02);
140+
break;
141+
case WB_KEYBOARD_LEFT:
142+
change_manual_steer_angle(-0.02);
143+
break;
144+
}
145+
}
146+
147+
void compute_gps_speed() {
148+
const double *coords = wb_gps_get_values(gps);
149+
const double vel[3] = {coords[X] - gps_coords[X], coords[Y] - gps_coords[Y], coords[Z] - gps_coords[Z]};
150+
const double dist = sqrt(vel[X] * vel[X] + vel[Y] * vel[Y] + vel[Z] * vel[Z]);
151+
152+
// store into global variables
153+
gps_speed = dist / time_step * 3600.0;
154+
memcpy(gps_coords, coords, sizeof(gps_coords));
155+
156+
// printf("gps speed: %g km/h\n", gps_speed);
157+
}
158+
159+
int main(int argc, char **argv) {
160+
wb_robot_init();
161+
162+
time_step = (int)wb_robot_get_basic_time_step();
163+
164+
// find wheels
165+
left_front_wheel = wb_robot_get_device("left_front_wheel");
166+
right_front_wheel = wb_robot_get_device("right_front_wheel");
167+
left_rear_wheel = wb_robot_get_device("left_rear_wheel");
168+
right_rear_wheel = wb_robot_get_device("right_rear_wheel");
169+
wb_motor_set_position(left_front_wheel, INFINITY);
170+
wb_motor_set_position(right_front_wheel, INFINITY);
171+
wb_motor_set_position(left_rear_wheel, INFINITY);
172+
wb_motor_set_position(right_rear_wheel, INFINITY);
173+
174+
// get steering motors
175+
left_steer = wb_robot_get_device("left_steer");
176+
right_steer = wb_robot_get_device("right_steer");
177+
178+
// camera device
179+
camera = wb_robot_get_device("camera");
180+
wb_camera_enable(camera, time_step);
181+
camera_width = wb_camera_get_width(camera);
182+
camera_height = wb_camera_get_height(camera);
183+
camera_fov = wb_camera_get_fov(camera);
184+
185+
// SICK sensor
186+
sick = wb_robot_get_device("Sick LMS 291");
187+
wb_lidar_enable(sick, time_step);
188+
sick_width = wb_lidar_get_horizontal_resolution(sick);
189+
sick_range = wb_lidar_get_max_range(sick);
190+
sick_fov = wb_lidar_get_fov(sick);
191+
192+
// initialize gps
193+
gps = wb_robot_get_device("gps");
194+
wb_gps_enable(gps, time_step);
195+
196+
// find lights
197+
left_flasher = wb_robot_get_device("left_flasher");
198+
right_flasher = wb_robot_get_device("right_flasher");
199+
tail_lights = wb_robot_get_device("tail_lights");
200+
work_head_lights = wb_robot_get_device("work_head_lights");
201+
road_head_lights = wb_robot_get_device("road_head_lights");
202+
203+
// start engine
204+
set_speed(10.0); // km/h
205+
206+
print_help();
207+
208+
// allow to switch to manual control
209+
wb_keyboard_enable(time_step);
210+
211+
// main loop
212+
while (wb_robot_step(time_step) != -1) {
213+
// get user input
214+
check_keyboard();
215+
216+
// read sensors
217+
// const unsigned char *camera_image = wb_camera_get_image(camera);
218+
// const float *sick_data = wb_lidar_get_range_image(sick);
219+
220+
// update stuff
221+
compute_gps_speed();
222+
blink_lights();
223+
}
224+
225+
wb_robot_cleanup();
226+
227+
return 0; // ignored
228+
}
46 KB
Binary file not shown.
Binary file not shown.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
% MATLAB controller for Webots
2+
% File: simulink_control.m
3+
% Date:
4+
% Description:
5+
% Author:
6+
% Modifications:
7+
8+
% uncomment the next two lines if you want to use
9+
% MATLAB's desktop to interact with the controller:
10+
% desktop;
11+
%keyboard;
12+
13+
TIME_STEP = 16;
14+
15+
FRONT_WHEEL_RADIUS = 0.38;
16+
REAR_WHEEL_RADIUS = 0.6;
17+
18+
19+
accelerometer_sensor=wb_robot_get_device('accelerometer');
20+
wb_accelerometer_enable(accelerometer_sensor,TIME_STEP);
21+
22+
lidar_sensor=wb_robot_get_device('Sick LMS 291');
23+
wb_lidar_enable(lidar_sensor,TIME_STEP);
24+
25+
compass_sensor=wb_robot_get_device('compass');
26+
wb_compass_enable(compass_sensor,TIME_STEP);
27+
28+
29+
gyro_sensor=wb_robot_get_device('gyro');
30+
wb_gyro_enable(gyro_sensor,TIME_STEP);
31+
32+
33+
gps_sensor=wb_robot_get_device('gps');
34+
wb_gps_enable(gps_sensor,TIME_STEP);
35+
36+
right_steer_sensor=wb_robot_get_device('right_steer_sensor');
37+
left_steer_sensor=wb_robot_get_device('left_steer_sensor');
38+
39+
wb_position_sensor_enable(right_steer_sensor,TIME_STEP);
40+
wb_position_sensor_enable(left_steer_sensor,TIME_STEP);
41+
42+
left_front_wheel = wb_robot_get_device('left_front_wheel');
43+
right_front_wheel = wb_robot_get_device('right_front_wheel');
44+
left_rear_wheel = wb_robot_get_device('left_rear_wheel');
45+
right_rear_wheel = wb_robot_get_device('right_rear_wheel');
46+
47+
48+
left_steer = wb_robot_get_device('left_steer');
49+
right_steer = wb_robot_get_device('right_steer');
50+
51+
left_flasher = wb_robot_get_device('left_flasher');
52+
right_flasher = wb_robot_get_device('right_flasher');
53+
tail_lights = wb_robot_get_device('tail_lights');
54+
work_head_lights = wb_robot_get_device('work_head_lights');
55+
road_head_lights = wb_robot_get_device('road_head_lights');
56+
57+
open_system('simulink_control');
58+
load_system('simulink_control');
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function result = wb_accelerometer_get_sampling_period(tag)
2+
% Usage: wb_accelerometer_get_sampling_period(tag)
3+
% Matlab API for Webots
4+
% Online documentation is available <a href="https://www.cyberbotics.com/doc/reference/accelerometer">here</a>
5+
6+
coder.extrinsic('calllib');
7+
result = calllib('libController', 'wb_accelerometer_get_sampling_period', tag);
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function result = wb_accelerometer_get_values(tag)
2+
% Usage: wb_accelerometer_get_values(tag)
3+
% Matlab API for Webots
4+
% Online documentation is available <a href="https://www.cyberbotics.com/doc/reference/accelerometer">here</a>
5+
6+
coder.extrinsic('calllib');
7+
coder.extrinsic('setdatatype');
8+
coder.extrinsic('get');
9+
coder.extrinsic('libpointer');
10+
11+
%obj=libpointer("doublePtr",[0,0,0])
12+
obj =calllib('libController', 'wb_accelerometer_get_values', tag);
13+
14+
15+
%setdatatype(obj,'doublePtr', 1, 3);
16+
%disp(obj(1));
17+
%disp(get(obj))
18+
setdatatype(obj, 'doublePtr', 1, 3); % 3 elemanlı bir double dizisi
19+
20+
values = get(obj,"Value"); % Verileri al
21+
22+
%disp(values);
23+
result = values;
24+
%result = get(obj, 'Value'); % Değerleri al
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function result = wb_compass_get_values(tag)
2+
% Usage: wb_compass_get_values(tag)
3+
% Matlab API for Webots
4+
% Online documentation is available <a href="https://www.cyberbotics.com/doc/reference/compass">here</a>
5+
6+
% This function retrieves the values from the compass sensor.
7+
coder.extrinsic('calllib');
8+
coder.extrinsic('setdatatype');
9+
coder.extrinsic('get');
10+
coder.extrinsic('libpointer');
11+
12+
obj = calllib('libController', 'wb_compass_get_values', tag);
13+
setdatatype(obj,'doublePtr', 1, 3);
14+
result = get(obj, 'Value');
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function result = wb_distance_sensor_get_value(tag)
2+
3+
coder.extrinsic('calllib');
4+
coder.extrinsic('setdatatype');
5+
coder.extrinsic('get');
6+
coder.extrinsic('libpointer');
7+
8+
9+
result = calllib('libController', 'wb_distance_sensor_get_value', tag);

0 commit comments

Comments
 (0)