Webots study notes (4) — steering wheel model

Hits: 0

Recently, I am learning the software of webots, and because the steering wheel is a hot spot in the Robocon event, I want to use the webots software to do a simple simulation of the steering wheel

steering wheel

I made a model of a four-wheeled car before. For the four steering wheel trains, four steering wheels are added, and each wheel train consists of a driving wheel and a steering wheel.

Analyze and build models

  • Under the robot node, create a body node as the chassis
  • Create a HingeJoint as the steering wheel of the first wheel train, use this as the parent node, and then create a Hingejoint as the driving wheel.
  • Adjust the coordinates of each node, the rotation axis and other parameters

Create a controller

#include <webots/robot.h>
#include <webots/motor.h>

#define TIME_STEP 64
#define num 3.1415926f/180.0f
float angle;

int main(int argc, char **argv) {

  wb_robot_init();
  WbDeviceTag lf_dir_motor
                    = wb_robot_get_device("lf_dir_motor");
  WbDeviceTag lf_val_motor
                    = wb_robot_get_device("lf_val_motor");
  wb_motor_set_position(lf_val_motor,INFINITY);
  wb_motor_set_velocity(lf_val_motor,0);


  WbDeviceTag lb_dir_motor
                    = wb_robot_get_device("lb_dir_motor");
  WbDeviceTag lb_val_motor
                    = wb_robot_get_device("lb_val_motor");
  wb_motor_set_position(lb_val_motor,INFINITY);
  wb_motor_set_velocity(lb_val_motor,0);


  WbDeviceTag rf_dir_motor
                    = wb_robot_get_device("rf_dir_motor");
  WbDeviceTag rf_val_motor
                    = wb_robot_get_device("rf_val_motor");
  wb_motor_set_position(rf_val_motor,INFINITY);
  wb_motor_set_velocity(rf_val_motor,0);

  WbDeviceTag rb_dir_motor
                    = wb_robot_get_device("rb_dir_motor");
  WbDeviceTag rb_val_motor
                    = wb_robot_get_device("rb_val_motor");
  wb_motor_set_position(rb_val_motor,INFINITY);
  wb_motor_set_velocity(rb_val_motor,0);

  while (wb_robot_step(TIME_STEP) != -1) {
  wb_motor_set_velocity(lf_val_motor,-20);
  wb_motor_set_velocity(rf_val_motor,-20);
  wb_motor_set_velocity(lb_val_motor,-20);
  wb_motor_set_velocity(rb_val_motor,-20);

  for(int i =0;i<360;i++)
    {
      angle = -i * num ;
      wb_motor_set_position(lf_dir_motor,angle);
      wb_motor_set_position(rf_dir_motor,angle);
      wb_motor_set_position(lb_dir_motor,angle);
      wb_motor_set_position(rb_dir_motor,angle);
      wb_robot_step(1);
    }
  for(int i =360;i>0;i--)
    {
      angle = -i * num ;
      wb_motor_set_position(lf_dir_motor,angle);
      wb_motor_set_position(rf_dir_motor,angle);
      wb_motor_set_position(lb_dir_motor,angle);
      wb_motor_set_position(rb_dir_motor,angle);
      wb_robot_step(1);
    }

  };

  wb_robot_cleanup();

  return 0;
}


Leave a Reply

Your email address will not be published.