/** * @brief provides imu readings in a 50 Hz rate. * */ void loop() { if((millis()-timer)>=20) { // Main loop runs at 50Hz timer=millis(); //Read data from the hardware gyro.read(); compass.readAcc(); compass.readMag(); //Assign read data to the ros messages imu_msg.angular_velocity.x=gyro.g.x; imu_msg.angular_velocity.y=gyro.g.y; imu_msg.angular_velocity.z=gyro.g.z; imu_msg.linear_acceleration.x=compass.a.x; imu_msg.linear_acceleration.y=compass.a.y; imu_msg.linear_acceleration.z=compass.a.z; mag_msg.magnetic_field.x=compass.m.x; mag_msg.magnetic_field.y=compass.m.y; mag_msg.magnetic_field.z=compass.m.z; //Publish the data to the ros message system imu_pub.publish( &imu_msg ); mag_pub.publish( &mag_msg); nh.spinOnce(); } nh.spinOnce(); }
// Reads x,y and z accelerometer registers void Read_Accel() { compass.readAcc(); AN[3] = compass.a.x; AN[4] = compass.a.y; AN[5] = compass.a.z; accel_x = SENSOR_SIGN[3] * (AN[3] - AN_OFFSET[3]); accel_y = SENSOR_SIGN[4] * (AN[4] - AN_OFFSET[4]); accel_z = SENSOR_SIGN[5] * (AN[5] - AN_OFFSET[5]); }
// Reads x,y and z accelerometer registers void Read_Accel() { compass.readAcc(); AN[3] = compass.a.x >> 4; // shift left 4 bits to use 12-bit representation (1 g = 256) AN[4] = compass.a.y >> 4; AN[5] = compass.a.z >> 4; accel_x = SENSOR_SIGN[3] * (AN[3] - AN_OFFSET[3]); accel_y = SENSOR_SIGN[4] * (AN[4] - AN_OFFSET[4]); accel_z = SENSOR_SIGN[5] * (AN[5] - AN_OFFSET[5]); }