目录
1.安装与修改语言
2.运行demo,了解功能
3.创建简单的仿真
5.添加激光雷达节点
官网下载过慢
参考:https://blog.csdn.net/weixin_44504987/article/details/104753615
最新版Webots R2020a Windows版本软件下载链接 (百度网盘)提取码: hui1
双击安装
打开选择classic经典,其他的都关掉,不在提示。
点击暂停,选择Tools->
英语改为汉语,点击yes,重启。
点击引导之旅
点击箭头,随便双击一个,如第一个,键盘上下左右控制运动
选取pioneer 3AT.wbt,此仿真以pioneer 3at为移动平台搭载激光雷达,进行运动控制。
左侧1-2-3-4-5----是柱子
左侧如下图为车体,点击编辑即可编辑程序,不建议编辑,主要是了解整体的仿真流程
点击暂停,向导,新项目目录
新世界,点击viewpoint,点击“加号”
1.添加地面
添加机器人
设置地面大小和高度
车体,控制方式,点击选择,选择lidar,也可选择matlab,点击编辑即可进行程序编写
如果pioneer3at_obstacle_avoidance_with_lidar 需要添加Lidar传感器,不然会报如下错误,会一直向前走,无避障功能,新建机器人控制,将pioneer3at_obstacle_avoidance_with_lidar复制到新建的my_controler中
用户手册,点击help
转化为节点,保存
参考:https://blog.csdn.net/crp997576280/article/details/105667450
添加结构
左键点击liodar,按住shift,点击绿色向上,可将其拖出来
命名:Sick 291
显示激光雷达
改激光雷达的名字
/*
* Copyright 1996-2019 Cyberbotics Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Description: Example of Sick LMS 291.
* The velocity of each wheel is set
* according to a Braitenberg-like algorithm which takes the values returned by the Sick as input.
*/
#include
#include
#include
#include
#include
#include
#define TIME_STEP 32
#define MAX_SPEED 6.4
#define CRUISING_SPEED 5.0
#define OBSTACLE_THRESHOLD 0.1
#define DECREASE_FACTOR 0.9
#define BACK_SLOWDOWN 0.9
// gaussian function
double gaussian(double x, double mu, double sigma) {
return (1.0 / (sigma * sqrt(2.0 * M_PI))) * exp(-((x - mu) * (x - mu)) / (2 * sigma * sigma));
}
int main(int argc, char **argv) {
// init webots stuff
wb_robot_init();
// get devices
WbDeviceTag lms291 = wb_robot_get_device("Sick 291");
WbDeviceTag front_left_wheel = wb_robot_get_device("front left wheel");
WbDeviceTag front_right_wheel = wb_robot_get_device("front right wheel");
WbDeviceTag back_left_wheel = wb_robot_get_device("back left wheel");
WbDeviceTag back_right_wheel = wb_robot_get_device("back right wheel");
// init lms291
wb_lidar_enable(lms291, TIME_STEP);
const int lms291_width = wb_lidar_get_horizontal_resolution(lms291);
const int half_width = lms291_width / 2;
const int max_range = wb_lidar_get_max_range(lms291);
const double range_threshold = max_range / 20.0;
const float *lms291_values = NULL;
// init braitenberg coefficient
double *const braitenberg_coefficients = (double *)malloc(sizeof(double) * lms291_width);
int i, j;
for (i = 0; i < lms291_width; ++i)
braitenberg_coefficients[i] = gaussian(i, half_width, lms291_width / 5);
// init motors
wb_motor_set_position(front_left_wheel, INFINITY);
wb_motor_set_position(front_right_wheel, INFINITY);
wb_motor_set_position(back_left_wheel, INFINITY);
wb_motor_set_position(back_right_wheel, INFINITY);
// init speed for each wheel
double back_left_speed = 0.0, back_right_speed = 0.0;
double front_left_speed = 0.0, front_right_speed = 0.0;
wb_motor_set_velocity(front_left_wheel, front_left_speed);
wb_motor_set_velocity(front_right_wheel, front_right_speed);
wb_motor_set_velocity(back_left_wheel, back_left_speed);
wb_motor_set_velocity(back_right_wheel, back_right_speed);
// init dynamic variables
double left_obstacle = 0.0, right_obstacle = 0.0;
// control loop
while (wb_robot_step(TIME_STEP) != -1) {
// get lidar values
lms291_values = wb_lidar_get_range_image(lms291);
// apply the braitenberg coefficients on the resulted values of the lms291
// near obstacle sensed on the left side
for (i = 0; i < half_width; ++i) {
if (lms291_values[i] < range_threshold) // far obstacles are ignored
left_obstacle += braitenberg_coefficients[i] * (1.0 - lms291_values[i] / max_range);
// near obstacle sensed on the right side
j = lms291_width - i - 1;
if (lms291_values[j] < range_threshold)
right_obstacle += braitenberg_coefficients[i] * (1.0 - lms291_values[j] / max_range);
}
// overall front obstacle
const double obstacle = left_obstacle + right_obstacle;
// compute the speed according to the information on
// obstacles
if (obstacle > OBSTACLE_THRESHOLD) {
const double speed_factor = (1.0 - DECREASE_FACTOR * obstacle) * MAX_SPEED / obstacle;
front_left_speed = speed_factor * left_obstacle;
front_right_speed = speed_factor * right_obstacle;
back_left_speed = BACK_SLOWDOWN * front_left_speed;
back_right_speed = BACK_SLOWDOWN * front_right_speed;
} else {
back_left_speed = CRUISING_SPEED;
back_right_speed = CRUISING_SPEED;
front_left_speed = CRUISING_SPEED;
front_right_speed = CRUISING_SPEED;
}
// set actuators
wb_motor_set_velocity(front_left_wheel, front_left_speed);
wb_motor_set_velocity(front_right_wheel, front_right_speed);
wb_motor_set_velocity(back_left_wheel, back_left_speed);
wb_motor_set_velocity(back_right_wheel, back_right_speed);
// reset dynamic variables to zero
left_obstacle = 0.0;
right_obstacle = 0.0;
}
free(braitenberg_coefficients);
wb_robot_cleanup();
return 0;
}
点击保存,生成
检测最大距离要够,避障阈值const double range_threshold = max_range / 20.0,要大于车体长度/2,不然转不过去
最后效果,灰色线是超声波模块/绿线是雷达,移动平台向前走,自主避障