Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To add to gvdgoorn comments: 1- The code that you already use to read sensor data can be implemented as a "node" in ROS, with few minor tweaks. I will provide an example code below.
2- As to what ROS brings to the table: The way I see it, ROS is at simplest a platform for process communication. Most of the work required for the communication part is done by ROS, so you can focus on writing the processes (AKA nodes).
For example, in your project you might have a node that reads sensor data -let's call it read_temp and another that processes it (process_temp). You write the code for each node and set them up such that the read_temp sends data to process_temp.
This node read gyro and accelerometer data from a MPU9250 sensor. I'm using a library called FaBo9Axis_MPU9250 written for raspberry pi to do exactly that. Note that the library itself has nothing to do with ROS-it's simply an interface to the sensor.

#!/usr/bin/env python
from math import pi
import rospy
import FaBo9Axis_MPU9250
from sensor_msgs.msg import Imu

mpu9250 = FaBo9Axis_MPU9250.MPU9250()
rospy.init_node('imu')

pub = rospy.Publisher('imu/data_raw',Imu, queue_size = 1)

msg = Imu()
msg.orientation_covariance[0] = -1
msg.header.frame_id = 'imu'
rate = rospy.Rate(100)

while not rospy.is_shutdown():
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()

    msg.header.stamp = rospy.Time.now()

    msg.linear_acceleration.x = accel['x']*9.81
    msg.linear_acceleration.y = accel['y']*9.81
    msg.linear_acceleration.z = accel['z']*9.81

    msg.angular_velocity.x = (gyro['x'] - 0.9)*0.01745329251
    msg.angular_velocity.y = (gyro['y'] - 0.5)*0.01745329251
    msg.angular_velocity.z = gyro['z']*0.01745329251
    pub.publish(msg)
    rate.sleep()

To add to gvdgoorn comments: comments:
1- The code that you already use to read sensor data can be implemented as a "node" in ROS, with few minor tweaks. I will provide an example code below.
2- As to what ROS brings to the table: The way I see it, ROS is at simplest a platform for process communication. Most of the work required for the communication part is done by ROS, so you can focus on writing the processes (AKA nodes).
For example, in your project you might have a node that reads sensor data -let's call it read_temp and another that processes it (process_temp). You write the code for each node and set them up such that the read_temp sends data to process_temp.
This node read gyro and accelerometer data from a MPU9250 sensor. I'm using a library called FaBo9Axis_MPU9250 written for raspberry pi to do exactly that. Note that the library itself has nothing to do with ROS-it's simply an interface to the sensor.

#!/usr/bin/env python
from math import pi
import rospy
import FaBo9Axis_MPU9250
from sensor_msgs.msg import Imu

mpu9250 = FaBo9Axis_MPU9250.MPU9250()
rospy.init_node('imu')

pub = rospy.Publisher('imu/data_raw',Imu, queue_size = 1)

msg = Imu()
msg.orientation_covariance[0] = -1
msg.header.frame_id = 'imu'
rate = rospy.Rate(100)

while not rospy.is_shutdown():
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()

    msg.header.stamp = rospy.Time.now()

    msg.linear_acceleration.x = accel['x']*9.81
    msg.linear_acceleration.y = accel['y']*9.81
    msg.linear_acceleration.z = accel['z']*9.81

    msg.angular_velocity.x = (gyro['x'] - 0.9)*0.01745329251
    msg.angular_velocity.y = (gyro['y'] - 0.5)*0.01745329251
    msg.angular_velocity.z = gyro['z']*0.01745329251
    pub.publish(msg)
    rate.sleep()

To add to gvdgoorn comments:
1- The code that you already use to read sensor data can be implemented as a "node" in ROS, with few minor tweaks. I will provide an example code below.

2- As to what ROS brings to the table: The way I see it, ROS is at its simplest a platform for process communication. Most of the work required for the communication part is done by ROS, so you can focus on writing the processes (AKA nodes).
For example, in your project you might have a node that reads sensor data -let's call it read_temp and another that processes it (process_temp). You write the code for each node and set them up such that the read_temp sends data to process_temp.

This node read gyro and accelerometer data from a MPU9250 sensor. I'm using a library called FaBo9Axis_MPU9250 written for raspberry pi to do exactly that. Note that the library itself has nothing to do with ROS-it's simply an interface to the sensor.

#!/usr/bin/env python
from math import pi
import rospy
import FaBo9Axis_MPU9250
from sensor_msgs.msg import Imu

mpu9250 = FaBo9Axis_MPU9250.MPU9250()
rospy.init_node('imu')

pub = rospy.Publisher('imu/data_raw',Imu, queue_size = 1)

msg = Imu()
msg.orientation_covariance[0] = -1
msg.header.frame_id = 'imu'
rate = rospy.Rate(100)

while not rospy.is_shutdown():
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()

    msg.header.stamp = rospy.Time.now()

    msg.linear_acceleration.x = accel['x']*9.81
    msg.linear_acceleration.y = accel['y']*9.81
    msg.linear_acceleration.z = accel['z']*9.81

    msg.angular_velocity.x = (gyro['x'] - 0.9)*0.01745329251
    msg.angular_velocity.y = (gyro['y'] - 0.5)*0.01745329251
    msg.angular_velocity.z = gyro['z']*0.01745329251
    pub.publish(msg)
    rate.sleep()

To add to gvdgoorn comments:
1- The code that you already use to read sensor data can be implemented as a "node" in ROS, with few minor tweaks. I will provide an example code below.

2- As to what ROS brings to the table: The way I see it, ROS is at its simplest a platform for process communication. Most of the work required for the communication part is done by ROS, so you can focus on writing the processes (AKA nodes).
For example, in your project you might have a node that reads sensor data -let's call it read_temp - and another that processes it (process_temp). You write the code for each node and set them up such that the read_temp sends data to process_temp.

This node read gyro and accelerometer data from a MPU9250 sensor. I'm using a library called FaBo9Axis_MPU9250 written for raspberry pi to do exactly that. Note that the library itself has nothing to do with ROS-it's simply an interface to the sensor.

#!/usr/bin/env python
from math import pi
import rospy
import FaBo9Axis_MPU9250
from sensor_msgs.msg import Imu

mpu9250 = FaBo9Axis_MPU9250.MPU9250()
rospy.init_node('imu')

pub = rospy.Publisher('imu/data_raw',Imu, queue_size = 1)

msg = Imu()
msg.orientation_covariance[0] = -1
msg.header.frame_id = 'imu'
rate = rospy.Rate(100)

while not rospy.is_shutdown():
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()

    msg.header.stamp = rospy.Time.now()

    msg.linear_acceleration.x = accel['x']*9.81
    msg.linear_acceleration.y = accel['y']*9.81
    msg.linear_acceleration.z = accel['z']*9.81

    msg.angular_velocity.x = (gyro['x'] - 0.9)*0.01745329251
    msg.angular_velocity.y = (gyro['y'] - 0.5)*0.01745329251
    msg.angular_velocity.z = gyro['z']*0.01745329251
    pub.publish(msg)
    rate.sleep()

To add to gvdgoorn comments:
1- The code that you already use to read sensor data can be implemented as a "node" in ROS, with few minor tweaks. I will provide an example code below.

2- As to what ROS brings to the table: The way I see it, ROS is at its simplest a platform for process communication. communication. Most of the work required for the communication part is done by ROS, so you can focus on writing the processes (AKA nodes).
For example, in your project you might have a node that reads sensor data -let's call it read_temp- and another that processes it (process_temp). You write the code for each node independently and set them up such that the read_temp sends data to process_temp: ROS facilitates modularity.

This node read gyro and accelerometer data from a MPU9250 sensor. I'm using a library called FaBo9Axis_MPU9250 written for raspberry pi to do exactly that. Note that the library itself has nothing to do with ROS-it's simply an interface to the sensor.

#!/usr/bin/env python
from math import pi
import rospy
import FaBo9Axis_MPU9250
from sensor_msgs.msg import Imu

mpu9250 = FaBo9Axis_MPU9250.MPU9250()
rospy.init_node('imu')

pub = rospy.Publisher('imu/data_raw',Imu, queue_size = 1)

msg = Imu()
msg.orientation_covariance[0] = -1
msg.header.frame_id = 'imu'
rate = rospy.Rate(100)

while not rospy.is_shutdown():
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()

    msg.header.stamp = rospy.Time.now()

    msg.linear_acceleration.x = accel['x']*9.81
    msg.linear_acceleration.y = accel['y']*9.81
    msg.linear_acceleration.z = accel['z']*9.81

    msg.angular_velocity.x = (gyro['x'] - 0.9)*0.01745329251
    msg.angular_velocity.y = (gyro['y'] - 0.5)*0.01745329251
    msg.angular_velocity.z = gyro['z']*0.01745329251
    pub.publish(msg)
    rate.sleep()

To add to gvdgoorn gvdhoorn's comments:
1- The code that you already use to read sensor data can be implemented as a "node" in ROS, with few minor tweaks. I will provide an example code below.

2- As to what ROS brings to the table: The way I see it, ROS is at its simplest a platform for process communication. Most of the work required for the communication part is done by ROS, so you can focus on writing the processes (AKA nodes).
For example, in your project you might have a node that reads sensor data -let's call it read_temp- and another that processes it (process_temp). You write the code for each node independently and set them up such that the read_temp sends data to process_temp: ROS facilitates modularity.

This node read reads gyro and accelerometer data from a MPU9250 sensor. I'm using a library called FaBo9Axis_MPU9250 written for raspberry pi to do exactly that. Note that the library itself has nothing to do with ROS-it's simply an interface to the sensor.

#!/usr/bin/env python
from math import pi
import rospy
import FaBo9Axis_MPU9250
from sensor_msgs.msg import Imu

mpu9250 = FaBo9Axis_MPU9250.MPU9250()
rospy.init_node('imu')

pub = rospy.Publisher('imu/data_raw',Imu, queue_size = 1)

msg = Imu()
msg.orientation_covariance[0] = -1
msg.header.frame_id = 'imu'
rate = rospy.Rate(100)

while not rospy.is_shutdown():
    accel = mpu9250.readAccel()
    gyro = mpu9250.readGyro()

    msg.header.stamp = rospy.Time.now()

    msg.linear_acceleration.x = accel['x']*9.81
    msg.linear_acceleration.y = accel['y']*9.81
    msg.linear_acceleration.z = accel['z']*9.81

    msg.angular_velocity.x = (gyro['x'] - 0.9)*0.01745329251
    msg.angular_velocity.y = (gyro['y'] - 0.5)*0.01745329251
    msg.angular_velocity.z = gyro['z']*0.01745329251
    pub.publish(msg)
    rate.sleep()