-
Notifications
You must be signed in to change notification settings - Fork 0
/
IMU_-_Ros.ino
71 lines (53 loc) · 1.53 KB
/
IMU_-_Ros.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* rosserial IMU Example
*
* This example is for the
*/
#include <ros.h>
#include <ros/time.h>
#include <sensor_msgs/Imu.h>
#include <Wire.h>
#include "SparkFun_BNO080_Arduino_Library.h"
BNO080 myIMU;
int sample_time = 50;
//Declarations
ros::NodeHandle nh;
sensor_msgs::Imu imu_msg;
ros::Publisher pub_ext_imu( "/ext_imu", &imu_msg); //Create topic
char frameid[] = "/ext_imu";
void setup()
{
// For I2C communication with IMU
Wire.begin();
myIMU.begin();
Wire.setClock(400000); //Increase I2C data rate to 400kHz
//Enable sensors
myIMU.enableAccelerometer(sample_time);
myIMU.enableGyro(sample_time);
myIMU.enableMagnetometer(sample_time);
nh.initNode();
nh.advertise(pub_ext_imu); //tell the world that the topic exist
imu_msg.header.frame_id = frameid;
}
void loop()
{
if (myIMU.dataAvailable() == true){
//Magnetometer(using quaterion orientation as placeholder)
imu_msg.orientation.x = myIMU.getMagX();
imu_msg.orientation.y = myIMU.getMagY();
imu_msg.orientation.z = myIMU.getMagZ();
//Angular velocity
imu_msg.angular_velocity.x = myIMU.getGyroX();
imu_msg.angular_velocity.y = myIMU.getGyroY();
imu_msg.angular_velocity.z = myIMU.getGyroZ();
//Accelertion
imu_msg.linear_acceleration.x = myIMU.getAccelX();
imu_msg.linear_acceleration.y = myIMU.getAccelY();
imu_msg.linear_acceleration.z = myIMU.getAccelZ();
//Setting timestamp
imu_msg.header.stamp = nh.now();
//Publish the message
pub_ext_imu.publish(&imu_msg);
}
nh.spinOnce();
}