Skip to content

Commit fac7852

Browse files
author
ValentinPitre
committed
Add motor test sequence as default autorun sequence
1 parent d1aaea7 commit fac7852

File tree

10 files changed

+397
-35
lines changed

10 files changed

+397
-35
lines changed

niryo_one_driver/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ find_package(catkin REQUIRED COMPONENTS
1717
niryo_one_msgs
1818
mcp_can_rpi
1919
dynamixel_sdk
20+
urdf
2021
)
2122

2223
find_package(Boost REQUIRED COMPONENTS system)
@@ -37,7 +38,7 @@ catkin_package(
3738

3839
include_directories(include ${catkin_INCLUDE_DIRS})
3940

40-
add_executable(niryo_one_driver
41+
add_executable(${PROJECT_NAME}
4142
src/utils/change_hardware_version.cpp
4243
src/utils/motor_offset_file_handler.cpp
4344
src/hw_driver/niryo_one_can_driver.cpp
@@ -48,6 +49,7 @@ add_executable(niryo_one_driver
4849
src/hw_comm/can_communication.cpp
4950
src/hw_comm/niryo_one_communication.cpp
5051
src/hw_comm/fake_communication.cpp
52+
src/test_motors.cpp
5153
src/ros_interface.cpp
5254
src/rpi_diagnostics.cpp
5355
src/niryo_one_hardware_interface.cpp
@@ -74,5 +76,6 @@ else()
7476
)
7577
endif()
7678

77-
add_dependencies(niryo_one_driver niryo_one_msgs_gencpp)
78-
79+
add_dependencies(${PROJECT_NAME}
80+
${catkin_EXPORTED_TARGETS}
81+
)

niryo_one_driver/include/niryo_one_driver/ros_interface.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "niryo_one_driver/communication_base.h"
3030
#include "niryo_one_driver/rpi_diagnostics.h"
3131
#include "niryo_one_driver/change_hardware_version.h"
32+
#include "niryo_one_driver/test_motors.h"
3233

3334
#include "niryo_one_msgs/SetInt.h"
3435
#include "niryo_one_msgs/SetLeds.h"
@@ -68,11 +69,15 @@ class RosInterface {
6869
RpiDiagnostics* rpi_diagnostics;
6970
ros::NodeHandle nh_;
7071

72+
NiryoOneTestMotor test_motor;
73+
7174
bool* flag_reset_controllers;
7275
int hardware_version;
7376
bool learning_mode_on;
7477
int calibration_needed;
78+
bool calibration_in_progress;
7579
bool last_connection_up_flag;
80+
int motor_test_status;
7681

7782
std::string rpi_image_version;
7883
std::string ros_niryo_one_version;
@@ -111,6 +116,8 @@ class RosInterface {
111116
ros::ServiceServer calibrate_motors_server;
112117
ros::ServiceServer request_new_calibration_server;
113118

119+
ros::ServiceServer test_motors_server;
120+
114121
ros::ServiceServer activate_learning_mode_server;
115122
ros::ServiceServer activate_leds_server;
116123

@@ -131,6 +138,7 @@ class RosInterface {
131138
ros::ServiceServer update_conveyor_id_server;
132139

133140
// callbacks
141+
bool callbackTestMotors(niryo_one_msgs::SetInt::Request &req, niryo_one_msgs::SetInt::Response &res);
134142

135143
bool callbackCalibrateMotors(niryo_one_msgs::SetInt::Request &req, niryo_one_msgs::SetInt::Response &res);
136144
bool callbackRequestNewCalibration(niryo_one_msgs::SetInt::Request &req, niryo_one_msgs::SetInt::Response &res);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef NIRYO_TEST_MOTORS_H
2+
#define NIRYO_TEST_MOTORS_H
3+
4+
5+
#include <boost/shared_ptr.hpp>
6+
#include <ros/ros.h>
7+
#include <string>
8+
#include <thread>
9+
#include <queue>
10+
#include <functional>
11+
#include <vector>
12+
13+
#include <urdf/model.h>
14+
15+
#include <std_msgs/Float64MultiArray.h>
16+
#include <std_msgs/Int64MultiArray.h>
17+
// #include <moveit/move_group_interface/move_group_interface.h>
18+
19+
#include <actionlib/client/simple_action_client.h>
20+
#include <control_msgs/FollowJointTrajectoryAction.h>
21+
22+
#include "niryo_one_msgs/SetBool.h"
23+
#include "niryo_one_msgs/SetInt.h"
24+
25+
#include <std_msgs/Empty.h>
26+
#include <sensor_msgs/JointState.h>
27+
28+
29+
typedef actionlib::SimpleActionClient< control_msgs::FollowJointTrajectoryAction > TrajClient;
30+
31+
class NiryoOneTestMotor {
32+
33+
private:
34+
ros::NodeHandle nh;
35+
ros::ServiceClient calibrate_motor_client;
36+
ros::Subscriber joint_state_subscriber;
37+
38+
ros::Publisher reset_stepper_publisher;
39+
40+
TrajClient* traj_client_;
41+
42+
std::vector<double> pose_start{0.0, 0.0, 0.3, 0.0, 0.0, 0.0};
43+
44+
bool enable_test;
45+
int _n_joints = 6;
46+
std::vector<std::string> _joint_names;
47+
std::vector<double> _joint_upper_limits;
48+
std::vector<double> _joint_lower_limits;
49+
std::vector<double> _joint_has_position_limits;
50+
51+
std::vector<double> _current_joint_pose;
52+
53+
54+
public:
55+
NiryoOneTestMotor();
56+
57+
void callbackJointSate(const sensor_msgs::JointState& msg);
58+
59+
bool getJointsLimits();
60+
61+
bool runTest(int nb_loops);
62+
void stopTest();
63+
void startTrajectory(control_msgs::FollowJointTrajectoryGoal goal);
64+
bool playTrajectory(control_msgs::FollowJointTrajectoryGoal goal);
65+
66+
control_msgs::FollowJointTrajectoryGoal armExtensionTrajectory(std::vector<double> joint_positions);
67+
actionlib::SimpleClientGoalState getState();
68+
69+
};
70+
#endif

niryo_one_driver/package.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<build_depend>trajectory_msgs</build_depend>
2626
<build_depend>dynamixel_sdk</build_depend>
2727
<build_depend>mcp_can_rpi</build_depend>
28+
<build_depend>urdf</build_depend>
2829

2930
<run_depend>roscpp</run_depend>
3031
<run_depend>rospy</run_depend>
@@ -42,6 +43,7 @@
4243
<run_depend>geometry_msgs</run_depend>
4344
<run_depend>sensor_msgs</run_depend>
4445
<run_depend>trajectory_msgs</run_depend>
46+
<run_depend>urdf</run_depend>
4547

4648
<export>
4749
</export>

niryo_one_driver/src/ros_interface.cpp

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,49 @@ RosInterface::RosInterface(CommunicationBase* niryo_one_comm, RpiDiagnostics* rp
4545
calibration_needed = 0;
4646
}
4747

48+
bool RosInterface::callbackTestMotors(niryo_one_msgs::SetInt::Request &req, niryo_one_msgs::SetInt::Response &res)
49+
{
50+
if (motor_test_status==1)
51+
{
52+
test_motor.stopTest();
53+
learning_mode_on = true;
54+
comm->activateLearningMode(learning_mode_on);
55+
return true;
56+
}
57+
58+
motor_test_status = 1;
59+
if (calibration_needed)
60+
{
61+
learning_mode_on = false;
62+
comm->activateLearningMode(learning_mode_on);
63+
64+
int calibration_mode = 1;
65+
std::string result_message = "";
66+
int result = comm->allowMotorsCalibrationToStart(calibration_mode, result_message);
67+
68+
ros::Duration(1).sleep();
69+
while (calibration_in_progress) { ros::Duration(0.05).sleep();}
70+
71+
learning_mode_on = true;
72+
ros::Duration(1).sleep();
73+
}
74+
75+
learning_mode_on = false;
76+
comm->activateLearningMode(learning_mode_on);
77+
78+
bool status = test_motor.runTest(req.value);
79+
80+
learning_mode_on = true;
81+
comm->activateLearningMode(learning_mode_on);
82+
83+
motor_test_status = status ? 0 : -1;
84+
res.status = status ? 200 : 400;
85+
res.message = status ? "Success" : "Fail";
86+
87+
return true;
88+
}
89+
90+
4891
bool RosInterface::callbackCalibrateMotors(niryo_one_msgs::SetInt::Request &req, niryo_one_msgs::SetInt::Response &res)
4992
{
5093
int calibration_mode = req.value;
@@ -230,6 +273,8 @@ void RosInterface::startServiceServers()
230273
calibrate_motors_server = nh_.advertiseService("niryo_one/calibrate_motors", &RosInterface::callbackCalibrateMotors, this);
231274
request_new_calibration_server = nh_.advertiseService("niryo_one/request_new_calibration", &RosInterface::callbackRequestNewCalibration, this);
232275

276+
test_motors_server = nh_.advertiseService("niryo_one/test_motors", &RosInterface::callbackTestMotors, this);
277+
233278
activate_learning_mode_server = nh_.advertiseService("niryo_one/activate_learning_mode", &RosInterface::callbackActivateLearningMode, this);
234279
activate_leds_server = nh_.advertiseService("niryo_one/set_dxl_leds", &RosInterface::callbackActivateLeds, this);
235280

@@ -260,7 +305,7 @@ void RosInterface::publishHardwareStatus()
260305
ros::Time time_now = ros::Time::now();
261306

262307
bool connection_up = false;
263-
bool calibration_in_progress = false;
308+
264309
std::string error_message;
265310
std::vector<std::string> motor_names;
266311
std::vector<std::string> motor_types;
@@ -287,6 +332,10 @@ void RosInterface::publishHardwareStatus()
287332
msg.rpi_temperature = rpi_diagnostics->getRpiCpuTemperature();
288333
msg.hardware_version = hardware_version;
289334
msg.connection_up = connection_up;
335+
if (motor_test_status<0)
336+
{
337+
error_message += " motor test error";
338+
}
290339
msg.error_message = error_message;
291340
msg.calibration_needed = calibration_needed;
292341
msg.calibration_in_progress = calibration_in_progress;

0 commit comments

Comments
 (0)