Skip to content
This repository was archived by the owner on Dec 1, 2020. It is now read-only.

Commit 89eaac6

Browse files
Merge pull request #169 from project-march/feature/PM-64-add-imu-node
Feature: add imu node
2 parents 6e743aa + 13ed675 commit 89eaac6

File tree

144 files changed

+19802
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

144 files changed

+19802
-0
lines changed

march_imu_manager/CMakeLists.txt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
cmake_minimum_required(VERSION 2.8.3)
2+
project(march_imu_manager)
3+
4+
add_compile_options(-std=c++11 -Wall -Wextra -g)
5+
6+
find_package(catkin REQUIRED COMPONENTS
7+
roscpp
8+
roslint
9+
sensor_msgs
10+
)
11+
12+
catkin_package(
13+
INCLUDE_DIRS include
14+
LIBRARIES ${PROJECT_NAME}
15+
CATKIN_DEPENDS
16+
sensor_msgs
17+
)
18+
19+
include_directories(
20+
include
21+
lib/include
22+
${catkin_INCLUDE_DIRS}
23+
)
24+
25+
file(GLOB_RECURSE lintfiles
26+
"src/*.cpp"
27+
"src/*.h"
28+
"test/*.cpp"
29+
)
30+
31+
roslint_cpp(${lintfiles})
32+
33+
set(xsensdeviceapi_LOCATION ${PROJECT_SOURCE_DIR}/lib/libxsensdeviceapi.so)
34+
set(xstypes_LOCATION ${PROJECT_SOURCE_DIR}/lib/libxstypes.so)
35+
36+
find_library(xsensdeviceapi_LIBRARY NAMES xsensdeviceapi PATHS ${PROJECT_SOURCE_DIR}/lib)
37+
find_library(xstypes_LIBRARY NAMES xstypes PATHS ${PROJECT_SOURCE_DIR}/lib)
38+
39+
add_library(xsensdeviceapi SHARED IMPORTED)
40+
set_property(TARGET xsensdeviceapi PROPERTY IMPORTED_LOCATION ${xsensdeviceapi_LOCATION})
41+
42+
add_library(xstypes SHARED IMPORTED)
43+
set_property(TARGET xstypes PROPERTY IMPORTED_LOCATION ${xstypes_LOCATION})
44+
45+
add_library(${PROJECT_NAME}
46+
src/Mtw.cpp
47+
src/WirelessMaster.cpp
48+
)
49+
50+
target_link_libraries(${PROJECT_NAME}
51+
${xsensdeviceapi_LIBRARY}
52+
${xstypes_LIBRARY}
53+
${catkin_LIBRARIES}
54+
)
55+
56+
add_executable(${PROJECT_NAME}_node src/main.cpp)
57+
58+
target_link_libraries(${PROJECT_NAME}_node
59+
${PROJECT_NAME}
60+
${xsensdeviceapi_LIBRARY}
61+
${xstypes_LIBRARY}
62+
${catkin_LIBRARIES}
63+
)
64+
65+
install(
66+
FILES
67+
${xsensdeviceapi_LOCATION}
68+
${xsensdeviceapi_LOCATION}.4
69+
${xsensdeviceapi_LOCATION}.4.6.0
70+
${xstypes_LOCATION}
71+
${xstypes_LOCATION}.4
72+
${xstypes_LOCATION}.4.6.0
73+
DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
74+
)
75+
76+
install(DIRECTORY include/${PROJECT_NAME}
77+
DESTINATION ${CATKIN_PACKAGE_INCLUDE_DESTINATION}
78+
)
79+
install(TARGETS ${PROJECT_NAME}
80+
LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION}
81+
)
82+
install(TARGETS ${PROJECT_NAME}_node
83+
RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
84+
)
85+
86+
if(CATKIN_ENABLE_TESTING)
87+
catkin_add_gtest(wireless_master_test test/WirelessMasterTest.cpp)
88+
target_link_libraries(wireless_master_test
89+
${PROJECT_NAME}
90+
${xsensdeviceapi_LIBRARY}
91+
${xstypes_LIBRARY}
92+
${catkin_LIBRARIES}
93+
)
94+
endif()
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include <deque>
4+
#include <mutex>
5+
6+
#include <ros/ros.h>
7+
8+
#include <xsensdeviceapi.h>
9+
#include <xstypes.h>
10+
11+
class Mtw : public XsCallback
12+
{
13+
public:
14+
Mtw(XsDevice* device, size_t maxBufferSize = 100);
15+
16+
/**
17+
* Returns whether any new packets are available.
18+
*/
19+
bool dataAvailable();
20+
21+
/**
22+
* Returns the oldest packet received from the MTw.
23+
* Does not delete it.
24+
*/
25+
const XsDataPacket* getOldestPacket();
26+
27+
/**
28+
* Deletes the oldest packet received from the MTw.
29+
*/
30+
void deleteOldestPacket();
31+
32+
/**
33+
* Returns the device id of the MTw. This id can also be found on the
34+
* back of the device.
35+
*/
36+
const XsDeviceId getId() const;
37+
38+
protected:
39+
/**
40+
* Callback when new packets are available. This runs in a seperate
41+
* thread and only stores the packets in the buffer.
42+
*/
43+
virtual void onLiveDataAvailable(XsDevice* device, const XsDataPacket* packet);
44+
45+
private:
46+
/**
47+
* Configures the MTw to output orientation, velocity and acceleration.
48+
*/
49+
void configure();
50+
51+
std::mutex m_mutex;
52+
53+
XsDevice* m_device;
54+
55+
size_t m_maxBufferSize;
56+
std::deque<XsDataPacket> m_packetBuffer;
57+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
3+
#include <condition_variable>
4+
#include <memory>
5+
#include <mutex>
6+
#include <unordered_map>
7+
8+
#include <ros/ros.h>
9+
10+
#include <xsensdeviceapi.h>
11+
#include <xstypes.h>
12+
13+
#include "march_imu_manager/Mtw.h"
14+
15+
/**
16+
* The wireless master class that connects to MTws and publishes the data on
17+
* '/march/imu/' ROS topics.
18+
*/
19+
class WirelessMaster : public XsCallback
20+
{
21+
public:
22+
WirelessMaster(ros::NodeHandle* node);
23+
~WirelessMaster();
24+
25+
/**
26+
* Finds and constructs a wireless master.
27+
* This method must be called first before everything else.
28+
*/
29+
int init();
30+
31+
/**
32+
* Configures the wireless master with given settings.
33+
* Can only be configured once init() succeeded.
34+
*
35+
* @param updateRate the desired update rate of the master in Hz.
36+
* @param channel the desired radio channel, defaults to 25.
37+
* @returns error code, 0 if successfull, -1 otherwise.
38+
*/
39+
int configure(const int updateRate, const int channel = 25);
40+
41+
/**
42+
* Waits for the given amount of MTws to connect.
43+
* Blocks the thread until the amount has connected.
44+
*
45+
* @param connections the amount of connections to wait for, defaults to 1.
46+
*/
47+
void waitForConnections(const size_t connections = 1);
48+
49+
/**
50+
* Starts the measurement of the MTw. This is required in order to
51+
* publish anything in the update loop. Once the measurement is started
52+
* no MTws will be able to connect.
53+
*
54+
* @returns true if successfull, false otherwise.
55+
*/
56+
bool startMeasurement();
57+
58+
/**
59+
* Returns whether the MTws are measuring.
60+
*/
61+
bool isMeasuring() const;
62+
63+
/**
64+
* Publishes all data from the MTws. This is supposed to be called in
65+
* an update loop.
66+
*/
67+
void update();
68+
69+
/**
70+
* Finds the closest supported update rate to the given desired rate.
71+
*
72+
* @param supportedUpdateRates rates that are supported by the master.
73+
* @param desiredUpdateRate rate that is desired.
74+
*/
75+
static int findClosestUpdateRate(const XsIntArray& supportedUpdateRates, const int desiredUpdateRate);
76+
77+
protected:
78+
/**
79+
* Callback for when new MTws connect or disconnect.
80+
* Runs in a seperate thread.
81+
*/
82+
virtual void onConnectivityChanged(XsDevice* dev, XsConnectivityState newState);
83+
84+
private:
85+
ros::NodeHandle* m_node;
86+
87+
std::mutex m_mutex;
88+
std::condition_variable m_cv;
89+
90+
XsControl* m_control = nullptr;
91+
XsDevicePtr m_master = nullptr;
92+
93+
std::unordered_map<uint32_t, std::unique_ptr<Mtw>> m_connectedMtws;
94+
std::unordered_map<uint32_t, ros::Publisher> m_publishers;
95+
};

0 commit comments

Comments
 (0)