-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathex_gravity_compensation_toggle.cpp
More file actions
146 lines (116 loc) · 4.46 KB
/
Copy pathex_gravity_compensation_toggle.cpp
File metadata and controls
146 lines (116 loc) · 4.46 KB
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
/**
* This file is a barebones skeleton of how to setup an arm for use.
* It demonstrates gravity compensation behavior by commanding torques
* equal to the force from gravity on the links and joints of an arm.
* Note that this only approximately balances out gravity, as imperfections in
* the torque sensing and modeled system can lead to "drift". Also, the
* particular choice of PID control gains can affect the performance of this
* demo.
*/
#include "group_command.hpp"
#include "group_feedback.hpp"
#include "robot_model.hpp"
#include "arm/arm.hpp"
#include "util/mobile_io.hpp"
#include <chrono>
#include "hebi_util.hpp"
using namespace hebi;
using namespace experimental;
int main(int argc, char* argv[])
{
//////////////////////////
///// Config Setup ///////
//////////////////////////
// Config file path
const std::string example_config_file = "config/ex_gravity_compensation_toggle.cfg.yaml";
std::vector<std::string> errors;
// Load the config
const auto example_config = RobotConfig::loadConfig(example_config_file, errors);
for (const auto& error : errors) {
std::cerr << error << std::endl;
}
if (!example_config) {
std::cerr << "Failed to load configuration from: " << example_config_file << std::endl;
return -1;
}
// For this demo, we need the arm and mobile_io
std::unique_ptr<arm::Arm> arm;
std::unique_ptr<hebi::util::MobileIO> mobile_io;
//////////////////////////
///// Arm Setup //////////
//////////////////////////
// Create the arm object from the configuration
arm = arm::Arm::create(*example_config);
// Keep retrying if arm not found
while (!arm) {
std::cerr << "Failed to create arm, retrying..." << std::endl;
// Retry
arm = arm::Arm::create(*example_config);
}
std::cout << "Arm connected." << std::endl;
// Retrieve the gravcomp plugin from the arm
// Get a weak_ptr from the arm API, lock it as a shared_ptr, and then downcast it from a general plugin pointer to a specific plugin pointer
auto plugin_shared_ptr = arm->getPluginByType<arm::plugin::GravityCompensationEffort>().lock();
if (!plugin_shared_ptr) {
std::cerr << "Failed to lock plugin shared_ptr. The plugin may have been destroyed." << std::endl;
return -1;
}
auto gravcomp_plugin_ptr = std::dynamic_pointer_cast<arm::plugin::GravityCompensationEffort>(plugin_shared_ptr);
if (!gravcomp_plugin_ptr) {
std::cerr << "Failed to cast plugin to GravityCompensationEffort." << std::endl;
return -1;
}
//////////////////////////
//// MobileIO Setup //////
//////////////////////////
// Create the mobile_io object from the configuration
std::cout << "Waiting for Mobile IO device to come online..." << std::endl;
mobile_io = createMobileIOFromConfig(*example_config);
// Keep retrying if Mobile IO not found
while (mobile_io == nullptr) {
std::cout << "Couldn't find Mobile IO. Check name, family, or device status..." << std::endl;
// Retry
mobile_io = createMobileIOFromConfig(*example_config);
}
std::cout << "Mobile IO connected." << std::endl;
// Clear any garbage on screen
mobile_io->clearText();
// Refresh mobile_io
auto last_state = mobile_io->update();
std::cout << "Commanded gravity-compensated zero force to the arm.\n"
<< " 🌍 (B2) - Toggles the gravity compensation on/off:\n"
<< " ON - Apply controller \n"
<< " OFF - Disable controller\n"
<< " ❌ (B1) - Exits the demo.\n";
//////////////////////////
//// Main Control Loop ///
//////////////////////////
while(arm->update())
{
auto updated_mobile_io = mobile_io->update(0);
if (updated_mobile_io)
{
/////////////////
// Button Presses
/////////////////
// Buttton B1 - End demo
if (mobile_io->getButtonDiff(1) == hebi::util::MobileIO::ButtonState::ToOn) {
// Clear MobileIO text
mobile_io->resetUI();
return 1;
}
// Button B2 - Set and unset gravcomp mode when button is pressed and released, respectively
if (mobile_io->getButtonDiff(2) == hebi::util::MobileIO::ButtonState::ToOn) {
// Enable gravcomp
gravcomp_plugin_ptr->setEnabled(true);
}
else if (mobile_io->getButtonDiff(2) == hebi::util::MobileIO::ButtonState::ToOff){
// Disable gravcomp
gravcomp_plugin_ptr->setEnabled(false);
}
}
// Send latest commands to the arm
arm->send();
}
return 0;
}