Example #1
0
int main(void) {
	// Create IP connection
	IPConnection ipcon;
	ipcon_create(&ipcon);

	// Create device object
	DistanceIR dir;
	distance_ir_create(&dir, UID, &ipcon);

	// Connect to brickd
	if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
		fprintf(stderr, "Could not connect\n");
		return 1;
	}
	// Don't use device before ipcon is connected

	// Get current distance (unit is mm)
	uint16_t distance;
	if(distance_ir_get_distance(&dir, &distance) < 0) {
		fprintf(stderr, "Could not get distance, probably timeout\n");
		return 1;
	}

	printf("Distance: %f cm\n", distance/10.0);

	printf("Press key to exit\n");
	getchar();
	distance_ir_destroy(&dir);
	ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
	return 0;
}
/*----------------------------------------------------------------------
 * publishRangeMessage()
 * Publish the Range message.
 *--------------------------------------------------------------------*/
void TinkerforgeSensors::publishRangeMessage(SensorDevice *sensor)
{
  if (sensor != NULL)
  {
    // generate Range message from distance sensor
    sensor_msgs::Range range_msg;

    uint16_t distance;
    if (sensor->getType() == DISTANCE_US_DEVICE_IDENTIFIER)
    {
      if(distance_us_get_distance_value((DistanceUS*)sensor->getDev(), &distance) < 0) {
        ROS_ERROR_STREAM("Could not get range us from " << sensor->getUID() << ", probably timeout");
        return;
      }
      range_msg.radiation_type = sensor_msgs::Range::ULTRASOUND;
      range_msg.range = distance;
      range_msg.field_of_view = 0.01;
      range_msg.min_range = 0.03;
      range_msg.max_range = 0.4;
    }
    else if (sensor->getType() == DISTANCE_IR_DEVICE_IDENTIFIER)
    {
      if(distance_ir_get_distance((DistanceIR*)sensor->getDev(), &distance) < 0) {
        ROS_ERROR_STREAM("Could not get range ir from " << sensor->getUID() << ", probably timeout");
        return;
      }
      range_msg.radiation_type = sensor_msgs::Range::INFRARED;
      range_msg.range = distance / 1000.0;
      range_msg.field_of_view = 0.01;
      range_msg.min_range = 0.03;
      range_msg.max_range = 0.4;
    }
    else
    {
      return;
    }

    // message header
    range_msg.header.seq =  sensor->getSeq();
    range_msg.header.stamp = ros::Time::now();
    range_msg.header.frame_id = sensor->getFrame();

    // publish Range msg to ros
    sensor->getPub().publish(range_msg);
  }
}