0

I am trying to add a distance sensor to a kuka robot in webots software KUKA robot that has no field called children but I added the distance sensor in bodyslot field now the sensor is reading nan all the time

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


#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <webots/distance_sensor.h>

#define TIME_STEP 32






int main(int argc, char **argv) {
 wb_robot_init();
//////////////////////////////

WbDeviceTag ps[1];
char ps_names[1][3] = {"DS"};


  ps[0] = wb_robot_get_device(ps_names[0]);
  wb_distance_sensor_enable(ps[0], TIME_STEP);

////////////////////////////////
// read sensors outputs
double ps_values;

  ps_values = wb_distance_sensor_get_value(ps[0]);
  //////////////////////////////////////////////////////////


  while (true) {
    step();

    printf ("\n %f \n", ps_values);
    //while (ps_values > 80){
    base_forwards_increment();
    //}
    
   // base_backwards_increment();
   
    
  }

  wb_robot_cleanup();

  return 0;
}

enter image description here To find a way to add a distance sensor to kuka robot

1 Answers1

0

It is correct to add new devices to the bodySlot field.

But you are retrieving the distance sensor values just once before starting the main simulation loop when the sensor is not active yet. Instead you should call the wb_distance_sensor_get_value() instruction just before the print instruction to get the current sensor measurement.

Here is the fixed code:


WbDeviceTag ps[1];
char ps_names[1][3] = {"DS"};


  ps[0] = wb_robot_get_device(ps_names[0]);
  wb_distance_sensor_enable(ps[0], TIME_STEP);

////////////////////////////////
// read sensors outputs
double ps_values;

  ps_values = wb_distance_sensor_get_value(ps[0]);
  //////////////////////////////////////////////////////////


  while (true) {
    step();

    ps_values = wb_distance_sensor_get_value(ps[0]); // READ SENSOR MEASUREMENT
    printf ("\n %f \n", ps_values);
    //while (ps_values > 80){
    base_forwards_increment();
    //}
    
   // base_backwards_increment();
   
    
  }

  wb_robot_cleanup();

  return 0;
}

Disclaimer: I am a Webots developer working at Cyberbotics.