reading sensor values

Discussion in 'Simulation' started by Michael West, Mar 27, 2019.

  1. Hello,

    Does anyone have any examples of how to read a sensor value established in your XML model. I have looked through the documentation and I believe the mjcb_sensor callback function is the function to do this. However, when I try and implement it it skips the function entirely.
    See Below:

    //read sensor callback
    void readSensor(const mjModel *m, mjData *d, int jj)
    {
    //mjtNum* sensordata;
    cout << "call back read" << endl;
    system("pause");
    }

    int main(int argc, const char** argv)
    {
    //install sensor read callback
    mjcb_sensor = readSensor;
    //install control callback
    mjcb_control = mycontroller;

    // run main loop, target real-time simulation and 60 fps rendering
    while (!glfwWindowShouldClose(window))
    {
    // advance interactive simulation for 1/60 sec
    // Assuming MuJoCo can simulate faster than real-time, which it usually can,
    // this loop will finish on time for the next frame to be rendered at 60 fps.
    // Otherwise add a cpu timer and exit this loop when it is time to render.
    mjtNum simstart = d->time;
    while (d->time - simstart < 1.0 / 60.0) {
    mj_step(m, d);
    jj++;
    }
    }
    }

    it never goes through the readSensor function
     
  2. Your code sample is somewhat incomplete. Anyway are you able to just directly read the sensor values from mjData?
    d->sensordata[n] will hold the nth sensor value.
     
  3. Hello, I am able to read the sensor data from mjData. However, with this method I am unable to determine which sensor I am reading.

    Also, I am aware the code sample is incomplete. However, I thought that was what was necessary to understand what issues I may be having trying to use the callback.
     
  4. Once you have the sensor ID (which you can obtain from the sensor name using mj_name2id) this is straightforward

    int adr = m->sensor_adr[sensorId];
    int dim = m->sensor_dim[sensorId];
    mjtNum sensor_data[dim];
    mju_copy(sensor_data, &d->sensordata[adr], dim);
     
  5. Thank you this worked