|
| 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 | +} |
0 commit comments