def main():
    rospy.init_node("imu")

    port = rospy.get_param("~port", "GPG3_AD1")
    sensor = InertialMeasurementUnit(bus=port)

    pub_imu = rospy.Publisher("~imu", Imu, queue_size=10)
    pub_temp = rospy.Publisher("~temp", Temperature, queue_size=10)
    pub_magn = rospy.Publisher("~magnetometer", MagneticField, queue_size=10)

    br = TransformBroadcaster()

    msg_imu = Imu()
    msg_temp = Temperature()
    msg_magn = MagneticField()
    hdr = Header(stamp=rospy.Time.now(), frame_id="IMU")

    rate = rospy.Rate(rospy.get_param('~hz', 30))
    while not rospy.is_shutdown():
        q = sensor.read_quaternion()  # x,y,z,w
        mag = sensor.read_magnetometer()  # micro Tesla (µT)
        gyro = sensor.read_gyroscope()  # deg/second
        accel = sensor.read_accelerometer()  # m/s²
        temp = sensor.read_temperature()  # °C

        msg_imu.header = hdr

        hdr.stamp = rospy.Time.now()

        msg_temp.header = hdr
        msg_temp.temperature = temp
        pub_temp.publish(msg_temp)

        msg_imu.header = hdr
        msg_imu.linear_acceleration.x = accel[0]
        msg_imu.linear_acceleration.y = accel[1]
        msg_imu.linear_acceleration.z = accel[2]
        msg_imu.angular_velocity.x = math.radians(gyro[0])
        msg_imu.angular_velocity.y = math.radians(gyro[1])
        msg_imu.angular_velocity.z = math.radians(gyro[2])
        msg_imu.orientation.w = q[3]
        msg_imu.orientation.x = q[0]
        msg_imu.orientation.y = q[1]
        msg_imu.orientation.z = q[2]
        pub_imu.publish(msg_imu)

        msg_magn.header = hdr
        msg_magn.magnetic_field.x = mag[0] * 1e-6
        msg_magn.magnetic_field.y = mag[1] * 1e-6
        msg_magn.magnetic_field.z = mag[2] * 1e-6
        pub_magn.publish(msg_magn)

        transform = TransformStamped(header=Header(stamp=rospy.Time.now(),
                                                   frame_id="world"),
                                     child_frame_id="IMU")
        transform.transform.rotation = msg_imu.orientation
        br.sendTransformMessage(transform)

        rate.sleep()
 def default(self, ci):
     """Publishes an Temperature message containing the current temperature
     and the variance (if a Gaussian noise modifier is applied.
     """
     msg = Temperature()
     msg.header = self.get_ros_header()
     msg.temperature = self.data["temperature"]
     msg.variance = self.get_variance(ci, "temperature")
     self.publish(msg)
def callback(data):
    global average, variance, pub
    rospy.loginfo('Temperature Received: %f', data.temperature)
    average = (average + data.temperature)/2
    variance = (variance + data.variance)/2

    t = Temperature()

    h = std_msgs.msg.Header()
    h.stamp = rospy.Time.now()
    
    t.header = h
    t.temperature = average
    t.variance = variance
    
    pub.publish(t)
Example #4
0
def publish_temp():
    pub = rospy.Publisher('henri/temperature', Temperature, queue_size=10)
    rospy.init_node('temperature_sensor_py', anonymous=True)
    rate = rospy.Rate(1)
    while not rospy.is_shutdown():
        t = Temperature()

        h = std_msgs.msg.Header()
        h.stamp = rospy.Time.now()
        h.frame_id = 'henri_link'

        t.header = h
        t.temperature = random.uniform(0.0, 30.0)
        t.variance = 0

        pub.publish(t)
        rospy.loginfo('Published Temp: %f', t.temperature)
        rate.sleep()
    def start(self):
        r = rospy.Rate(10)  # 10hz
        while not rospy.is_shutdown():
            self.ms5837.read()

            tempmsg = Temperature()
            tempmsg.header = Header()
            tempmsg.header.stamp = rospy.Time.now()
            tempmsg.temperature = self.ms5837.temperature()
            self.pub_temp.publish(tempmsg)

            depthmsg = FluidDepth()
            depthmsg.header = Header()
            depthmsg.header.stamp = rospy.Time.now()
            depthmsg.header.frame_id = "pressure_link"
            depthmsg.depth = self.ms5837.depth()
            self.pub_depth.publish(depthmsg)
            r.sleep()
Example #6
0
def publishBarometerData():
    airPressurePub = rospy.Publisher('air_pressure',
                                     FluidPressure,
                                     queue_size=10)
    temperaturePub = rospy.Publisher('barometer_temperature',
                                     Temperature,
                                     queue_size=10)
    rospy.init_node('barometer_publisher', anonymous=True)

    # Set update rate, in Hz
    rate = rospy.Rate(10)

    while not rospy.is_shutdown():
        # Pressure
        baro.refreshPressure()
        time.sleep(0.1)
        baro.readPressure()

        # Temperature
        baro.refreshTemperature()
        time.sleep(0.1)
        baro.readTemperature()

        # Process the data
        baro.calculatePressureAndTemperature()

        # Generate Header
        header = Header()
        header.stamp = rospy.Time.now()

        # Generate Message (Temperature)
        temperatureMsg = Temperature()
        temperatureMsg.header = header
        temperatureMsg.temperature = baro.TEMP
        temperaturePub.publish(temperatureMsg)

        # Generate message and publish (Pressure)
        airPressureMsg = FluidPressure()
        airPressureMsg.header = header
        airPressureMsg.fluid_pressure = baro.PRES * 100
        airPressurePub.publish(airPressureMsg)

        # Sleep
        rate.sleep()
Example #7
0
    def spin_once(self):
        def baroPressureToHeight(value):
            c1 = 44330.0
            c2 = 9.869232667160128300024673081668e-6
            c3 = 0.1901975534856
            intermediate = 1 - math.pow(c2 * value, c3)
            height = c1 * intermediate
            return height

        # get data
        data = self.mt.read_measurement()
        # common header
        h = Header()
        h.stamp = rospy.Time.now()
        h.frame_id = self.frame_id

        # get data (None if not present)
        temp_data = data.get('Temperature')  # float
        orient_data = data.get('Orientation Data')
        velocity_data = data.get('Velocity')
        position_data = data.get('Latlon')
        altitude_data = data.get('Altitude')
        acc_data = data.get('Acceleration')
        gyr_data = data.get('Angular Velocity')
        mag_data = data.get('Magnetic')
        pressure_data = data.get('Pressure')
        time_data = data.get('Timestamp')
        gnss_data = data.get('GNSS')

        # debug the data from the sensor
        # print(data)
        # print("\n")

        # create messages and default values
        "Temp message"
        temp_msg = Temperature()
        pub_temp = False
        "Imu message supported with Modes 1 & 2"
        imuraw_msg = Imu()
        pub_imuraw = False
        imuins_msg = Imu()
        pub_imuins = False
        "SensorSample message supported with Mode 2"
        ss_msg = sensorSample()
        pub_ss = False
        "Mag message supported with Modes 1 & 2"
        mag_msg = Vector3Stamped()
        pub_mag = False
        "Baro in meters"
        baro_msg = FluidPressure()
        height_msg = baroSample()
        pub_baro = False
        "GNSS message supported only with MTi-G-7xx devices"
        "Valid only for modes 1 and 2"
        gnssPvt_msg = gnssSample()
        gps1_msg = NavSatFix()
        gps2_msg = GPSFix()
        pub_gnssPvt = False
        gnssSatinfo_msg = GPSStatus()
        pub_gnssSatinfo = False
        # All filter related outputs
        "Supported in mode 3"
        ori_msg = orientationEstimate()
        pub_ori = False
        "Supported in mode 3 for MTi-G-7xx devices"
        vel_msg = velocityEstimate()
        pub_vel = False
        "Supported in mode 3 for MTi-G-7xx devices"
        pos_msg = positionEstimate()
        pub_pos = False

        # first getting the sampleTimeFine
        # note if we are not using ros time, the we should replace the header
        # with the time supplied by the GNSS unit
        if time_data and not self.use_rostime:
            time = time_data['SampleTimeFine']
            h.stamp.secs = 100e-6 * time
            h.stamp.nsecs = 1e5 * time - 1e9 * math.floor(h.stamp.secs)

        # temp data
        if temp_data:
            temp_msg.temperature = temp_data['Temp']
            pub_temp = True

        # acceleration data
        if acc_data:
            if 'accX' in acc_data:  # found acceleration
                pub_imuraw = True
                imuraw_msg.linear_acceleration.x = acc_data['accX']
                imuraw_msg.linear_acceleration.y = acc_data['accY']
                imuraw_msg.linear_acceleration.z = acc_data['accZ']
            if 'freeAccX' in acc_data:  # found free acceleration
                pub_imuins = True
                imuins_msg.linear_acceleration.x = acc_data['freeAccX']
                imuins_msg.linear_acceleration.y = acc_data['freeAccY']
                imuins_msg.linear_acceleration.z = acc_data['freeAccZ']
            if 'Delta v.x' in acc_data:  # found delta-v's
                pub_ss = True
                ss_msg.internal.imu.dv.x = acc_data['Delta v.x']
                ss_msg.internal.imu.dv.y = acc_data['Delta v.y']
                ss_msg.internal.imu.dv.z = acc_data['Delta v.z']
            #else:
            #	raise MTException("Unsupported message in XDI_AccelerationGroup.")

        # gyro data
        if gyr_data:
            if 'gyrX' in gyr_data:  # found rate of turn
                pub_imuraw = True
                imuraw_msg.angular_velocity.x = gyr_data['gyrX']
                imuraw_msg.angular_velocity.y = gyr_data['gyrY']
                imuraw_msg.angular_velocity.z = gyr_data['gyrZ']
                # note we do not force publishing the INS if we do not use the free acceleration
                imuins_msg.angular_velocity.x = gyr_data['gyrX']
                imuins_msg.angular_velocity.y = gyr_data['gyrY']
                imuins_msg.angular_velocity.z = gyr_data['gyrZ']
            if 'Delta q0' in gyr_data:  # found delta-q's
                pub_ss = True
                ss_msg.internal.imu.dq.w = gyr_data['Delta q0']
                ss_msg.internal.imu.dq.x = gyr_data['Delta q1']
                ss_msg.internal.imu.dq.y = gyr_data['Delta q2']
                ss_msg.internal.imu.dq.z = gyr_data['Delta q3']
            #else:
            #	raise MTException("Unsupported message in XDI_AngularVelocityGroup.")

        # magfield
        if mag_data:
            ss_msg.internal.mag.x = mag_msg.vector.x = mag_data['magX']
            ss_msg.internal.mag.y = mag_msg.vector.y = mag_data['magY']
            ss_msg.internal.mag.z = mag_msg.vector.z = mag_data['magZ']
            pub_mag = True

        if pressure_data:
            pub_baro = True
            baro_msg.fluid_pressure = pressure_data['Pressure']
            height = baroPressureToHeight(pressure_data['Pressure'])
            height_msg.height = ss_msg.internal.baro.height = height

        # gps fix message
        if gnss_data and 'lat' in gnss_data:
            pub_gnssPvt = True
            # A "3" means that the MTi-G is using the GPS data.
            # A "1" means that the MTi-G was using GPS data and is now coasting/dead-reckoning the
            # 	position based on the inertial sensors (the MTi-G is not using GPS data in this mode).
            # 	This is done for 45 seconds, before the MTi-G Mode drops to "0".
            # A "0" means that the MTi-G doesn't use GPS data and also that it
            # 	doesn't output position based on the inertial sensors.
            if gnss_data['fix'] < 2:
                gnssSatinfo_msg.status = NavSatStatus.STATUS_NO_FIX  # no fix
                gps1_msg.status.status = NavSatStatus.STATUS_NO_FIX  # no fix
                gps1_msg.status.service = 0
            else:
                gnssSatinfo_msg.status = NavSatStatus.STATUS_FIX  # unaugmented
                gps1_msg.status.status = NavSatStatus.STATUS_FIX  # unaugmented
                gps1_msg.status.service = NavSatStatus.SERVICE_GPS
            # lat lon alt
            gps1_msg.latitude = gnss_data['lat']
            gps1_msg.longitude = gnss_data['lon']
            gps1_msg.altitude = gnss_data['hEll']
            # covariances
            gps1_msg.position_covariance[0] = math.pow(gnss_data['horzAcc'], 2)
            gps1_msg.position_covariance[4] = math.pow(gnss_data['horzAcc'], 2)
            gps1_msg.position_covariance[8] = math.pow(gnss_data['vertAcc'], 2)
            gps1_msg.position_covariance_type = NavSatFix.COVARIANCE_TYPE_DIAGONAL_KNOWN
            # custom message
            gnssPvt_msg.itow = gnss_data['iTOW']
            gnssPvt_msg.fix = gnss_data['fix']
            gnssPvt_msg.latitude = gnss_data['lat']
            gnssPvt_msg.longitude = gnss_data['lon']
            gnssPvt_msg.hEll = gnss_data['hEll']
            gnssPvt_msg.hMsl = gnss_data['hMsl']
            gnssPvt_msg.vel.x = gnss_data['velE']
            gnssPvt_msg.vel.y = gnss_data['velN']
            gnssPvt_msg.vel.z = gnss_data['velD']
            gnssPvt_msg.hAcc = gnss_data['horzAcc']
            gnssPvt_msg.vAcc = gnss_data['vertAcc']
            gnssPvt_msg.sAcc = gnss_data['speedAcc']
            gnssPvt_msg.pDop = gnss_data['PDOP']
            gnssPvt_msg.hDop = gnss_data['HDOP']
            gnssPvt_msg.vDop = gnss_data['VDOP']
            gnssPvt_msg.numSat = gnss_data['nSat']
            gnssPvt_msg.heading = gnss_data['heading']
            gnssPvt_msg.headingAcc = gnss_data['headingAcc']

        if orient_data:
            if 'Q0' in orient_data:
                pub_imuraw = True
                imuraw_msg.orientation.w = orient_data['Q0']
                imuraw_msg.orientation.x = orient_data['Q1']
                imuraw_msg.orientation.y = orient_data['Q2']
                imuraw_msg.orientation.z = orient_data['Q3']
                pub_imuins = True
                imuins_msg.orientation.w = orient_data['Q0']
                imuins_msg.orientation.x = orient_data['Q1']
                imuins_msg.orientation.y = orient_data['Q2']
                imuins_msg.orientation.z = orient_data['Q3']
            elif 'Roll' in orient_data:
                pub_ori = True
                ori_msg.roll = orient_data['Roll']
                ori_msg.pitch = orient_data['Pitch']
                ori_msg.yaw = orient_data['Yaw']
            else:
                raise MTException(
                    'Unsupported message in XDI_OrientationGroup')

        if velocity_data:
            pub_vel = True
            vel_msg.velE = velocity_data['velX']
            vel_msg.velN = velocity_data['velY']
            vel_msg.velU = velocity_data['velZ']

        if position_data:
            pub_pos = True
            pos_msg.latitude = position_data['lat']
            pos_msg.longitude = position_data['lon']

        if altitude_data:
            pub_pos = True
            tempData = altitude_data['ellipsoid']
            pos_msg.hEll = tempData[0]

        # publish available information
        if pub_imuraw:
            imuraw_msg.header = h
            self.imuraw_pub.publish(imuraw_msg)
        if pub_imuins:
            imuins_msg.header = h
            self.imuins_pub.publish(imuins_msg)
        if pub_mag:
            mag_msg.header = h
            self.mag_pub.publish(mag_msg)
        if pub_temp:
            temp_msg.header = h
            self.temp_pub.publish(temp_msg)
        if pub_ss:
            ss_msg.header = h
            self.ss_pub.publish(ss_msg)
        if pub_baro:
            baro_msg.header = h
            height_msg.header = h
            self.baro_pub.publish(baro_msg)
            self.height_pub.publish(height_msg)
        if pub_gnssPvt:
            gnssPvt_msg.header = h
            gps1_msg.header = h
            self.gnssPvt_pub.publish(gnssPvt_msg)
            self.gps1_pub.publish(gps1_msg)
        #if pub_gnssSatinfo:
        #	gnssSatinfo_msg.header = h
        #	self.gnssSatinfo_pub.publish(gnssSatinfo_msg)
        if pub_ori:
            ori_msg.header = h
            self.ori_pub.publish(ori_msg)
        if pub_vel:
            vel_msg.header = h
            self.vel_pub.publish(vel_msg)
        if pub_pos:
            pos_msg.header = h
            self.pos_pub.publish(pos_msg)
Example #8
0
                            '!f', orientation_hex[46:54].decode("hex"))[0]
                        xsensVel.z = struct.unpack(
                            '!f', orientation_hex[54:62].decode("hex"))[0]

                        imuData.angular_velocity.x = struct.unpack(
                            '!f', orientation_hex[68:76].decode("hex"))[0]
                        imuData.angular_velocity.y = struct.unpack(
                            '!f', orientation_hex[76:84].decode("hex"))[0]
                        imuData.angular_velocity.z = struct.unpack(
                            '!f', orientation_hex[84:92].decode("hex"))[0]

                        magData.header = header
                        magData.magnetic_field.x = struct.unpack(
                            '!f', orientation_hex[98:106].decode("hex"))[0]
                        magData.magnetic_field.y = struct.unpack(
                            '!f', orientation_hex[106:114].decode("hex"))[0]
                        magData.magnetic_field.z = struct.unpack(
                            '!f', orientation_hex[114:122].decode("hex"))[0]

                        temperature.header = header
                        temperature.temperature = struct.unpack(
                            '!f', orientation_hex[128:136].decode("hex"))[0]
                        #print(temperature.temperature)
        ImuPub.publish(imuData)
        xsensVelPub.publish(xsensVel)
        tempPub.publish(temperature)
        magPub.publish(magData)

except KeyboardInterrupt:
    uart.close()
Example #9
0
    pubImu.publish(imu_msg)


    """ Temperature in degrees Celsius """
    # run some parts only on the real robot
    if hostname == 'minibot' or hostname == 'minibottest':
        temp = bno.read_temp()
    else:
        # test values only!
        temp = 1.0

    # Print
    # rospy.loginfo('Temperature: {}°C'.format(temp))

    # add header to temperature message
    temp_msg.header = h

    #
    # publish message
    #
    temp_msg.temperature = temp
    pubTemp.publish(temp_msg)


    # Other values you can optionally read:
    # Magnetometer data (in micro-Teslas):
    #x,y,z = bno.read_magnetometer()
    # Linear acceleration data (i.e. acceleration from movement, not gravity--
    # returned in meters per second squared):
    #x,y,z = bno.read_linear_acceleration()
    # Gravity acceleration data (i.e. acceleration just from gravity--returned