1111#include " arm/arm.hpp"
1212#include " util/mobile_io.hpp"
1313#include < chrono>
14- #include < iostream >
14+ #include " hebi_util.hpp "
1515
1616using namespace hebi ;
1717using namespace experimental ; // For all things mobileIO
@@ -48,44 +48,48 @@ int main(int argc, char* argv[])
4848 return -1 ;
4949 }
5050
51+ // For this demo, we need the arm and mobile_io
52+ std::unique_ptr<arm::Arm> arm;
53+ std::unique_ptr<hebi::util::MobileIO> mobile_io;
54+
5155 // ////////////////////////
5256 // /// Arm Setup //////////
5357 // ////////////////////////
5458
55- // Create the arm object from the configuration, and keep retrying if arm not found
56- auto arm = arm::Arm::create (*example_config);
59+ // Create the arm object from the configuration
60+ arm = arm::Arm::create (*example_config);
61+
62+ // Keep retrying if arm not found
5763 while (!arm) {
5864 std::cerr << " Failed to create arm, retrying..." << std::endl;
65+
66+ // Retry
5967 arm = arm::Arm::create (*example_config);
6068 }
6169 std::cout << " Arm connected." << std::endl;
6270
63-
6471 // ////////////////////////
6572 // // MobileIO Setup //////
6673 // ////////////////////////
6774
68- // Create the MobileIO object
69- std::unique_ptr<util::MobileIO> mobile_io = util::MobileIO::create (" Arm" , " mobileIO" );
70- if (!mobile_io)
71- {
72- std::cout << " couldn't find mobile IO device!\n " ;
73- return 1 ;
74- }
75+ // Create the mobile_io object from the configuration
76+ std::cout << " Waiting for Mobile IO device to come online..." << std::endl;
77+ mobile_io = createMobileIOFromConfig (*example_config);
7578
76- // Clear any garbage on screen
77- mobile_io->resetUI ();
79+ // Keep retrying if Mobile IO not found
80+ while (mobile_io == nullptr ) {
81+ std::cout << " Couldn't find Mobile IO. Check name, family, or device status..." << std::endl;
7882
79- // Setup instructions for display
80- std::string instructions ;
81- instructions = ( " B1 - Home Position \n B3 - AR Control Mode \n "
82- " B6 - Grav Comp Mode \n B8 - Quit \n " ) ;
83+ // Retry
84+ mobile_io = createMobileIOFromConfig (*example_config) ;
85+ }
86+ std::cout << " Mobile IO connected. " << std::endl ;
8387
84- // Display instructions on screen
85- mobile_io->appendText (instructions );
88+ // Clear any garbage on screen
89+ mobile_io->clearText ( );
8690
87- // Setup state variable for mobile device
88- auto last_mobile_state = mobile_io->update ();
91+ // Refresh mobile_io
92+ auto last_state = mobile_io->update ();
8993
9094 // ////////////////////////
9195 // // Main Control Loop ///
@@ -95,13 +99,15 @@ int main(int argc, char* argv[])
9599 bool softstart = true ;
96100 bool ar_mode = false ;
97101
98- // Make sure we softstart the arm first.
102+ // Load user data from config
99103 Eigen::VectorXd home_position (arm -> robotModel ().getDoFCount ());
100- home_position << 0 , M_PI/3 , 2 *M_PI/3 , 5 *M_PI/6 , -M_PI/2 , 0 ; // Adjust depending on your DoFs
104+ home_position = Eigen::Map<const Eigen::VectorXd>(example_config->getUserData ().getFloatList (" home_position" ).data (), example_config->getUserData ().getFloatList (" home_position" ).size ());
105+ double soft_start_time = example_config->getUserData ().getFloat (" homing_duration" );
106+ double xyz_scale = example_config->getUserData ().getFloat (" xyz_scale" );
101107
102108 // Command the softstart to home position
103109 arm -> update ();
104- arm -> setGoal (arm::Goal::createFromPosition (4 , home_position)); // take 4 seconds
110+ arm -> setGoal (arm::Goal::createFromPosition (soft_start_time , home_position)); // take 4 seconds
105111 arm -> send ();
106112
107113 // Get the cartesian position and rotation matrix @ home position
@@ -130,39 +136,36 @@ int main(int argc, char* argv[])
130136
131137 // Stay in softstart, don't do any other behavior
132138 continue ;
133- }
139+ }
134140
135141 // Get latest mobile_state
136142 auto updated_mobile_io = mobile_io->update (0 );
137143
138- if (!updated_mobile_io)
139- std::cout << " Failed to get feedback from mobile I/O; check connection!\n " ;
140- else
144+ if (updated_mobile_io)
141145 {
142146 // Button B1 - Return to home position
143- if (mobile_io->getButtonDiff (1 ) == util::MobileIO::ButtonState::ToOn) {
144- ar_mode = false ;
145- arm -> setGoal (arm::Goal::createFromPosition (4 , home_position));
147+ if (mobile_io->getButtonDiff (1 ) == hebi:: util::MobileIO::ButtonState::ToOn) {
148+ ar_mode = false ;
149+ arm -> setGoal (arm::Goal::createFromPosition (soft_start_time , home_position));
146150 }
147151
148152 // Button B3 - Start AR Control
149- if (mobile_io->getButtonDiff (3 ) == util::MobileIO::ButtonState::ToOn) {
153+ if (mobile_io->getButtonDiff (3 ) == hebi:: util::MobileIO::ButtonState::ToOn) {
150154 xyz_phone_init << mobile_io -> getLastFeedback ().mobile ().arPosition ().get ().getX (),
151155 mobile_io -> getLastFeedback ().mobile ().arPosition ().get ().getY (),
152156 mobile_io -> getLastFeedback ().mobile ().arPosition ().get ().getZ ();
153- std::cout << xyz_phone_init << std::endl;
154157 rot_phone_init = makeRotationMatrix (mobile_io -> getLastFeedback ().mobile ().arOrientation ().get ());
155158 ar_mode = true ;
156159 }
157160
158161 // Button B6 - Grav Comp Mode
159- if (mobile_io->getButtonDiff (6 ) == util::MobileIO::ButtonState::ToOn) {
162+ if (mobile_io->getButtonDiff (6 ) == hebi:: util::MobileIO::ButtonState::ToOn) {
160163 arm -> cancelGoal ();
161164 ar_mode = false ;
162165 }
163166
164167 // Button B8 - End Demo
165- if (mobile_io->getButtonDiff (8 ) == util::MobileIO::ButtonState::ToOn) {
168+ if (mobile_io->getButtonDiff (8 ) == hebi:: util::MobileIO::ButtonState::ToOn) {
166169 // Clear MobileIO text
167170 mobile_io->resetUI ();
168171 return 1 ;
@@ -178,9 +181,9 @@ int main(int argc, char* argv[])
178181 auto rot_phone = makeRotationMatrix (mobile_io->getLastFeedback ().mobile ().arOrientation ().get ());
179182
180183 // Calculate new targets
181- Eigen::Vector3d xyz_scale ;
182- xyz_scale << 1 , 1 , 2 ;
183- Eigen::Vector3d xyz_target = xyz_home + (0.75 * xyz_scale .array () *
184+ Eigen::Vector3d xyz_scale_vec ;
185+ xyz_scale_vec << 1 , 1 , 2 ;
186+ Eigen::Vector3d xyz_target = xyz_home + (xyz_scale * xyz_scale_vec .array () *
184187 (rot_phone_init.transpose () * (xyz_phone - xyz_phone_init)).array ()).matrix ();
185188 Eigen::Matrix3d rot_target = rot_phone_init.transpose () * rot_phone * rot_home;
186189
@@ -190,7 +193,7 @@ int main(int argc, char* argv[])
190193 rot_target);
191194
192195 // Create and send new goal to the arm
193- arm -> setGoal (arm::Goal::createFromPosition (target_joints));
196+ arm -> setGoal (arm::Goal::createFromPosition (example_config-> getUserData (). getFloat ( " latency " ), target_joints));
194197 }
195198
196199 // Send latest commands to the arm
0 commit comments