-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunilateral.cpp
169 lines (142 loc) · 4.77 KB
/
unilateral.cpp
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @file unilateral.cpp
* @author Guy Burroughes
* @brief The main file for the unilateral executable
* @version 0.1
* @date 2022-01-09
*
* @copyright Copyright (c) 2022
*
*/
/// \cond
#include <chrono>
#include <iostream>
#include <rclcpp/rclcpp.hpp>
#include <stdlib.h>
#include <string>
#include <thread>
#include <std_srvs/srv/trigger.hpp>
/// \endcond
#include "DemandSubscriber.h"
#include "Input.h"
#include "JointStatePublisher.h"
#include "KinovaControl.h"
#include "KinovaRobot.h"
#include "LoopCycle.h"
using namespace std;
/**
* @brief desired Position and Velocity for PD controller.
*
*/
Eigen7f firstPosition, firstVelocity;
auto home_local = std::make_shared< std::atomic<bool> >();
auto robot_start = std::make_shared< std::atomic<bool> >();
auto robot_stop = std::make_shared< std::atomic<bool> >();
void home_trigger(const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
std::shared_ptr<std_srvs::srv::Trigger::Response> response)
{
*home_local = true;
response->success = true;
}
void robot_start_trigger(const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
std::shared_ptr<std_srvs::srv::Trigger::Response> response)
{
*robot_start = true;
response->success = true;
}
void robot_stop_trigger(const std::shared_ptr<std_srvs::srv::Trigger::Request> request,
std::shared_ptr<std_srvs::srv::Trigger::Response> response)
{
*robot_stop = true;
response->success = true;
}
/*! \file */
/**
* @brief Main entry point of programm
*
* @param argc
* @param argv
* @return int
*/
int main(int argc, char *argv[]) {
// Set Environment variables
set_env_var();
// Initialise ROS2
rclcpp::init(argc, argv);
// Initialise variable
rclcpp::Node::SharedPtr node =
std::make_shared<rclcpp::Node>("kinova_unilateral");
RCLCPP_INFO(rclcpp::get_logger("Main"), "Connecting to robots.");
auto robot = std::make_shared<KinovaRobot>("robot", "192.168.1.16");
auto robotControl = std::make_shared<KinovaControl>("robot", robot, node);
rclcpp::executors::MultiThreadedExecutor executor;
auto robotPublisher = std::make_shared<JointStatePublisher>(robot);
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr service_home =
node->create_service<std_srvs::srv::Trigger>("home_robot", &home_trigger);
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr service_start =
node->create_service<std_srvs::srv::Trigger>("robot_start", &robot_start_trigger);
rclcpp::Service<std_srvs::srv::Trigger>::SharedPtr service_stop =
node->create_service<std_srvs::srv::Trigger>("robot_stop", &robot_stop_trigger);
executor.add_node(robotPublisher);
executor.add_node(node);
executor.add_node(robotControl->getPdController());
// Set up Loop cycle
std::shared_ptr<LoopCycle> loopCycle =
std::make_shared<LoopCycle>(node, robot, robotControl);
// Set Initial Demand
Eigen7f home = Eigen7f::Zero();
home[1] = 270;
home[3] = 90;
home[5] = 90;
firstPosition = home;
firstVelocity = Eigen7f::Zero();
loopCycle->setDemand(firstPosition, firstVelocity);
// Set up demand subscriber
auto demandSubscriber = std::make_shared<DemandSubscriber>(
"demandSubscriber", "demand", loopCycle);
executor.add_node(demandSubscriber);
// Declare ROS2 parameters
node->declare_parameter<bool>("lateral_run", false);
rclcpp::Parameter action_param("lateral_run", false);
while (rclcpp::ok()) {
// set stop parameter
action_param = rclcpp::Parameter("lateral_run", false);
node->set_parameter(action_param);
// Clear Faults
robot->clearFault();
// Home the robot
RCLCPP_INFO(rclcpp::get_logger("Main"), "Ready to home robot.");
RCLCPP_INFO(rclcpp::get_logger("Main"), "Home the robot? [Y/n]");
auto cont = spin_till_input(&executor, home_local);
if (!cont)
return 0;
RCLCPP_INFO(rclcpp::get_logger("Main"), "Homing robot.");
// Clear Faults
robot->clearFault();
robot->home();
// Clear Faults
robot->clearFault();
// Start Control
RCLCPP_INFO(rclcpp::get_logger("Main"), "Start control? [Y/n]");
cont = spin_till_input(&executor, robot_start);
if (!cont)
return 0;
RCLCPP_INFO(rclcpp::get_logger("Main"),
"Starting control unilateral robot.");
// Start loop thread
std::thread runner(&LoopCycle::loop, loopCycle);
// Spin ROS2 services
// executor.spin();
RCLCPP_INFO(rclcpp::get_logger("Main"), "Press enter to stop!");
//execute_till_keypress(&executor);
cont = spin_till_input(&executor, robot_stop);
RCLCPP_INFO(rclcpp::get_logger("Main"), "Pausing");
// set stop parameter
action_param = rclcpp::Parameter("lateral_run", true);
node->set_parameter(action_param);
// Stop threads and exit
runner.join();
}
rclcpp::shutdown();
return 0;
}