@@ -67,6 +67,12 @@ void PrimaryClient::start(const size_t max_num_tries, const std::chrono::millise
6767 pipeline_->run ();
6868}
6969
70+ void PrimaryClient::stop ()
71+ {
72+ pipeline_->stop ();
73+ stream_.close ();
74+ }
75+
7076void PrimaryClient::addPrimaryConsumer (std::shared_ptr<comm::IConsumer<PrimaryPackage>> primary_consumer)
7177{
7278 multi_consumer_->addConsumer (primary_consumer);
@@ -91,5 +97,68 @@ std::deque<ErrorCode> PrimaryClient::getErrorCodes()
9197 error_code_queue_.clear ();
9298 return error_codes;
9399}
100+
101+ bool PrimaryClient::sendScript (const std::string& program)
102+ {
103+ // urscripts (snippets) must end with a newline, or otherwise the controller's runtime will
104+ // not execute them. To avoid problems, we always just append a newline here, even if
105+ // there may already be one.
106+ auto program_with_newline = program + ' \n ' ;
107+
108+ size_t len = program_with_newline.size ();
109+ const uint8_t * data = reinterpret_cast <const uint8_t *>(program_with_newline.c_str ());
110+ size_t written;
111+
112+ const auto send_script_contents = [this , program_with_newline, data, len,
113+ &written](const std::string&& description) -> bool {
114+ if (stream_.write (data, len, written))
115+ {
116+ URCL_LOG_DEBUG (" Sent program to robot:\n %s" , program_with_newline.c_str ());
117+ return true ;
118+ }
119+ const std::string error_message = " Could not send program to robot: " + description;
120+ URCL_LOG_ERROR (error_message.c_str ());
121+ return false ;
122+ };
123+
124+ if (send_script_contents (" initial attempt" ))
125+ {
126+ return true ;
127+ }
128+
129+ if (reconnectStream ())
130+ {
131+ return send_script_contents (" after reconnecting primary stream" );
132+ }
133+
134+ return false ;
135+ }
136+
137+ bool PrimaryClient::reconnectStream ()
138+ {
139+ URCL_LOG_DEBUG (" Closing primary stream..." );
140+ stream_.close ();
141+ if (stream_.connect ())
142+ {
143+ URCL_LOG_DEBUG (" Primary stream connected" );
144+ return true ;
145+ }
146+ URCL_LOG_ERROR (" Failed to reconnect primary stream!" );
147+ return false ;
148+ }
149+
150+ bool PrimaryClient::checkCalibration (const std::string& checksum)
151+ {
152+ std::shared_ptr<primary_interface::KinematicsInfo> kin_info = consumer_->getKinematicsInfo ();
153+ while (kin_info == nullptr )
154+ {
155+ std::this_thread::sleep_for (std::chrono::seconds (1 ));
156+ kin_info = consumer_->getKinematicsInfo ();
157+ }
158+ URCL_LOG_DEBUG (" Got calibration information from robot." );
159+
160+ return kin_info->toHash () == checksum;
161+ }
162+
94163} // namespace primary_interface
95164} // namespace urcl
0 commit comments