<机器人技术创新与实践>课程-Webots自主研习
【摘要】 在进行本课程之前需要先完成:课程说明Gazebo仿真篇Webots是一款功能强大的跨平台机器人仿真软件,在启动并进入镜像之后,单击右侧第二个甲壳虫图标。下面简要介绍一下该机器人软件的功能。上方:菜单栏左侧:场景物品属性右侧:代码框下方:调试控制台中部:三维仿真全景+工具栏代码可以编译和调试,需要安装对应包,make等。支持Python、C++和Java等。具备完整稳定的ROS1和ROS2接口...
在进行本课程之前需要先完成:
Webots是一款功能强大的跨平台机器人仿真软件,在启动并进入镜像之后,单击右侧第二个甲壳虫图标。
下面简要介绍一下该机器人软件的功能。
- 上方:菜单栏
- 左侧:场景物品属性
- 右侧:代码框
- 下方:调试控制台
- 中部:三维仿真全景+工具栏
代码可以编译和调试,需要安装对应包,make等。支持Python、C++和Java等。
具备完整稳定的ROS1和ROS2接口。
案例非常豐富。
飛行器系列:
外星探测车系列:
#include <stdio.h>
#include <webots/keyboard.h>
#include <webots/motor.h>
#include <webots/robot.h>
#define TIME_STEP 64
#define VELOCITY 0.6
enum {
back_left_bogie,
front_left_bogie,
front_left_arm,
back_left_arm,
front_left_wheel,
middle_left_wheel,
back_left_wheel,
back_right_bogie,
front_right_bogie,
front_right_arm,
back_right_arm,
front_right_wheel,
middle_right_wheel,
back_right_wheel,
JOINTS_MAX
};
WbDeviceTag joints[JOINTS_MAX];
void move_4_wheels(double v) {
wb_motor_set_velocity(joints[front_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[middle_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[back_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[front_right_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[middle_right_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[back_right_wheel], v * VELOCITY);
wb_motor_set_available_torque(joints[middle_right_wheel], 0.0);
wb_motor_set_available_torque(joints[middle_left_wheel], 0.0);
}
void move_6_wheels(double v) {
wb_motor_set_available_torque(joints[middle_right_wheel], 2.0);
wb_motor_set_available_torque(joints[middle_left_wheel], 2.0);
wb_motor_set_velocity(joints[front_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[middle_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[back_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[front_right_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[middle_right_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[back_right_wheel], v * VELOCITY);
}
void turn_wheels_right() {
wb_motor_set_position(joints[front_left_arm], 0.4);
wb_motor_set_position(joints[front_right_arm], 0.227);
wb_motor_set_position(joints[back_right_arm], -0.227);
wb_motor_set_position(joints[back_left_arm], -0.4);
}
void turn_wheels_left() {
wb_motor_set_position(joints[front_left_arm], -0.227);
wb_motor_set_position(joints[front_right_arm], -0.4);
wb_motor_set_position(joints[back_right_arm], 0.4);
wb_motor_set_position(joints[back_left_arm], 0.227);
}
void wheels_straight() {
wb_motor_set_position(joints[front_left_arm], 0.0);
wb_motor_set_position(joints[front_right_arm], 0.0);
wb_motor_set_position(joints[back_right_arm], 0.0);
wb_motor_set_position(joints[back_left_arm], 0.0);
}
void turn_around(double v) {
wb_motor_set_position(joints[front_left_arm], -0.87);
wb_motor_set_position(joints[front_right_arm], 0.87);
wb_motor_set_position(joints[back_right_arm], -0.87);
wb_motor_set_position(joints[back_left_arm], 0.87);
wb_motor_set_velocity(joints[front_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[middle_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[back_left_wheel], v * VELOCITY);
wb_motor_set_velocity(joints[front_right_wheel], -v * VELOCITY);
wb_motor_set_velocity(joints[middle_right_wheel], -v * VELOCITY);
wb_motor_set_velocity(joints[back_right_wheel], -v * VELOCITY);
wb_motor_set_available_torque(joints[middle_right_wheel], 0.0);
wb_motor_set_available_torque(joints[middle_left_wheel], 0.0);
}
int main() {
// Required to initialize Webots
wb_robot_init();
joints[back_left_bogie] = wb_robot_get_device("BackLeftBogie");
joints[front_left_bogie] = wb_robot_get_device("FrontLeftBogie");
joints[front_left_arm] = wb_robot_get_device("FrontLeftArm");
joints[back_left_arm] = wb_robot_get_device("BackLeftArm");
joints[front_left_wheel] = wb_robot_get_device("FrontLeftWheel");
joints[middle_left_wheel] = wb_robot_get_device("MiddleLeftWheel");
joints[back_left_wheel] = wb_robot_get_device("BackLeftWheel");
joints[back_right_bogie] = wb_robot_get_device("BackRightBogie");
joints[front_right_bogie] = wb_robot_get_device("FrontRightBogie");
joints[front_right_arm] = wb_robot_get_device("FrontRightArm");
joints[back_right_arm] = wb_robot_get_device("BackRightArm");
joints[front_right_wheel] = wb_robot_get_device("FrontRightWheel");
joints[middle_right_wheel] = wb_robot_get_device("MiddleRightWheel");
joints[back_right_wheel] = wb_robot_get_device("BackRightWheel");
wb_motor_set_position(joints[front_left_wheel], INFINITY);
wb_motor_set_position(joints[middle_left_wheel], INFINITY);
wb_motor_set_position(joints[back_left_wheel], INFINITY);
wb_motor_set_position(joints[front_right_wheel], INFINITY);
wb_motor_set_position(joints[middle_right_wheel], INFINITY);
wb_motor_set_position(joints[back_right_wheel], INFINITY);
printf("Select the 3D window and use the keyboard to drive this robot:\n");
printf("\n");
printf("Q: forwards-left; W: forwards; E: forwards-right\n");
printf("S: spin counter-clockwise\n");
printf("Y: backwards-left; X: backwards; C: backwards-right\n");
wb_keyboard_enable(TIME_STEP);
// start moving
move_6_wheels(1.0);
while (wb_robot_step(TIME_STEP) != -1) {
int key = wb_keyboard_get_key();
switch (key) {
case 'W':
// forwards
wheels_straight();
move_6_wheels(1.0);
break;
case 'X':
// backwards
wheels_straight();
move_6_wheels(-1.0);
break;
case 'Q':
// forwards left
turn_wheels_left();
move_4_wheels(1.0);
break;
case 'E':
// forwards right
turn_wheels_right();
move_4_wheels(1.0);
break;
case 'Y':
// backwards left
turn_wheels_left();
move_4_wheels(-1.0);
break;
case 'C':
// backwards right
turn_wheels_right();
move_4_wheels(-1.0);
break;
case 'S':
// spin counter-clockwise
turn_around(1.0);
break;
}
}
wb_robot_cleanup();
return 0;
}
INFO: sojourner: Starting controller: /home/zhangrelay/webots/projects/robots/nasa/controllers/sojourner/sojourner
Select the 3D window and use the keyboard to drive this robot:
Q: forwards-left; W: forwards; E: forwards-right
S: spin counter-clockwise
Y: backwards-left; X: backwards; C: backwards-right
对应代码:
printf("Select the 3D window and use the keyboard to drive this robot:\n");
printf("\n");
printf("Q: forwards-left; W: forwards; E: forwards-right\n");
printf("S: spin counter-clockwise\n");
printf("Y: backwards-left; X: backwards; C: backwards-right\n");
多多尝试案例,并读懂代码,其他内容后续补充。
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)