当前位置:网站首页>Webots notes day 2
Webots notes day 2
2022-06-30 04:39:00 【Dream_ choe】
webots note the second day
1. Actuator( Actuator ) Use
- The actuator is similar to the sensor , The label used is also
Wb_Device_Tag, So is the calling functionwb_robot_get_device(), But there is no need to call the open functionwb_*_enable(). - For motion control , The action is broken down into several steps for control .
wb_motor_set_position()Request the corresponding position of the motor .
#include <webots/robot.h>
#include <webots/motor.h>
#include <math.h>
#define TIME_STEP 32
int main() {
wb_robot_init();
WbDeviceTag motor = wb_robot_get_device("my_motor");
double F = 2.0; // frequency 2 Hz
double t = 0.0; // elapsed simulation time
while (1) {
double pos = sin(t * 2.0 * M_PI * F);
wb_motor_set_position(motor, pos);
wb_robot_step(TIME_STEP);
t += (double)TIME_STEP / 1000.0;
}
return 0;
}
webots/motor.hSpeed of motor in , The acceleration , Definition of force and various parameters .wb_motor_set_position()Specify only the desired target value , Similar to a real robot , The rotation angle of the motor can not reach the expected value , Because the torque of the motor is not enough to resist gravity . If you want to control the rotation angle of multiple motors at the same time , It is necessary to set the expected value for the rotation angle of each motor .
2. wb_robot_step() Use
Webots There are two different uses , Control delay and analog delay .
- Control delay (
wb_robot_step()Use ) - Analog delay ( Definition in the scene tree :
WorldInfo.basicTimeStep)
Each controller needs to call wb_robot_step(), Otherwise, the sensor and actuator will not update the data and get stuck ( Only in synchronous mode )
3. Use sensors and actuators together
Webots And each robot controller runs in a separate process . such as , The simulation includes several robots , There are three processes in total : One is Webots Of , The other two are robotic . Each controller procedure calls wb_robot_step() Exchange sensor and actuator data . such as ,wb_motor_set_position You can't send data to Webots.
Look at the code below :
while(1){
double d1 = wb_distance_sensor_get_value(ds1);
double d2 = wb_distance_sensor_get_value(ds2);
if(d2<d1) //WRONG: d2 will always equal to d1 here
avoidCollision();
wb_robot_step(40);
}
Because there is no... Between the two sensor data reading functions wb_robot_step() function , So two values cannot be changed at the same time .
This is the right code :
while(1){
double d1 = wb_distance_sensor_get_value(ds1);
wb_robot_step();
double d2 = wb_distance_sensor_get_value(ds2);
if (d2<d1)
avoidCollision();
wb_robot_step();
}
However, it is generally recommended to include only one in the main control loop |wb_robot_step() Function to update all sensor and actuator data at the same time . as follows :
while(1){
readSensors();
actuateMotors();
wb_robot_step(TIME_STEP);
}
Here we can put wb_robot_step() Function at the beginning of the loop . This allows the sensor to have valid data before reading the sensor data . otherwise , The first iteration may be performed without the sensor being defined .
while(1){
wb_robot_step(TIME_STEP);
readSensors();
actuateMotors();
}
example :
#include <webots/robot.h>
#include <webots/differential_wheels.h>
#include <webots/distance_sensor.h>
#define TIME_STEP 32
int main() {
wb_robot_init();
WbDeviceTag left_sensor = wb_robot_get_device("left_sensor");
WbDeviceTag right_sensor = wb_robot_get_device("right_sensor");
wb_distance_sensor_enable(left_sensor, TIME_STEP);
wb_distance_sensor_enable(right_sensor, TIME_STEP);
while (1) {
wb_robot_step(TIME_STEP);
// read sensors
double left_dist = wb_distance_sensor_get_value(left_sensor);
double right_dist = wb_distance_sensor_get_value(right_sensor);
// compute behavior
double left = compute_left_speed(left_dist, right_dist);
double right = compute_right_speed(left_dist, right_dist);
// actuate wheel motors
wb_differential_wheels_set_speed(left, right);
}
return 0;
}
边栏推荐
- Learning about signals
- IO stream, character read / write, copy
- Foreign SSL certificate
- BeanFactory创建流程
- Static keyword
- Implementation of one interview question one distributed lock every day
- Matlab reads fig file and restores signal
- Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
- Bean creation process and lazy init delay loading mechanism
- Bean创建流程 与 lazy-init 延迟加载机制原理
猜你喜欢

Network layer protocol hardware

Redis implements SMS login function (II) redis implements login function
![Tea mall system based on SSM framework [project source code + database script + report]](/img/d9/0a46c0da9839a7186bd3a9ae55f0a5.png)
Tea mall system based on SSM framework [project source code + database script + report]

System programming summary

OneNote production schedule

破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)

【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc

Junior students summarize JS advanced interview questions

Myrpc version 2

SSL update method
随机推荐
FortiGate firewall and Aruze cloud tunnel interruption
This connection is not a private connection this website may be pretending to steal your personal or financial information
Qt6 QML Book/Qt Quick 3D/Qt Quick 3D
Differences between beanfactory and factorybean
Connect to the database and run node JS running database shows that the database is missing
Tea mall system based on SSM framework [project source code + database script + report]
Stack implementation integrated Calculator - code implementation
Detailed explanation of data link layer
Websocket implementation principle
Five methods to clear floating and their advantages and disadvantages
Wildcard SSL certificate issuing time
What to do when the alicloud SSL certificate expires
Beanfactory creation process
Es2016 key summary
什么是光耦电路,实际使用中应该注意些什么?
破局存量客群营销,试一下客户分群管理(含聚类模型等实操效果评估)
Threejs实现模拟河流,水面水流,水管水流,海面
【Paper】2006_ Time-Optimal Control of a Hovering Quad-Rotor Helicopter
Implementation steps of dynamic proxy
Myrpc version 0