commander.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017, 2018 Simon Rasmussen (refactor)
3  *
4  * Copyright 2015, 2016 Thomas Timm Andersen (original version)
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
20 #include "ur_modern_driver/log.h"
21 
22 bool URCommander::write(const std::string &s)
23 {
24  size_t len = s.size();
25  const uint8_t *data = reinterpret_cast<const uint8_t *>(s.c_str());
26  size_t written;
27  return stream_.write(data, len, written);
28 }
29 
30 void URCommander::formatArray(std::ostringstream &out, std::array<double, 6> &values)
31 {
32  std::string mod("[");
33  for (auto const &val : values)
34  {
35  out << mod << val;
36  mod = ",";
37  }
38  out << "]";
39 }
40 
41 bool URCommander::uploadProg(const std::string &s)
42 {
43  LOG_DEBUG("Sending program [%s]", s.c_str());
44  return write(s);
45 }
46 
47 bool URCommander::setToolVoltage(uint8_t voltage)
48 {
49  if (voltage != 0 || voltage != 12 || voltage != 24)
50  return false;
51 
52  std::ostringstream out;
53  out << "set_tool_voltage(" << (int)voltage << ")\n";
54  std::string s(out.str());
55  return write(s);
56 }
57 
58 bool URCommander::setFlag(uint8_t pin, bool value)
59 {
60  std::ostringstream out;
61  out << "set_flag(" << (int)pin << "," << (value ? "True" : "False") << ")\n";
62  std::string s(out.str());
63  return write(s);
64 }
65 bool URCommander::setPayload(double value)
66 {
67  std::ostringstream out;
68  out << "set_payload(" << std::fixed << std::setprecision(5) << value << ")\n";
69  std::string s(out.str());
70  return write(s);
71 }
72 
73 bool URCommander::stopj(double a)
74 {
75  std::ostringstream out;
76  out << "stopj(" << std::fixed << std::setprecision(5) << a << ")\n";
77  std::string s(out.str());
78  return write(s);
79 }
80 
81 bool URCommander_V1_X::speedj(std::array<double, 6> &speeds, double acceleration)
82 {
83  std::ostringstream out;
84  out << std::fixed << std::setprecision(5);
85  out << "speedj(";
86  formatArray(out, speeds);
87  out << "," << acceleration << "," << 0.02 << ")\n";
88  std::string s(out.str());
89  return write(s);
90 }
91 bool URCommander_V1_X::setAnalogOut(uint8_t pin, double value)
92 {
93  std::ostringstream out;
94  out << "sec io_fun():\n"
95  << "set_analog_out(" << (int)pin << "," << std::fixed << std::setprecision(4) << value << ")\n"
96  << "end\n";
97  std::string s(out.str());
98  return write(s);
99 }
100 
101 bool URCommander_V1_X::setDigitalOut(uint8_t pin, bool value)
102 {
103  std::ostringstream out;
104  out << "sec io_fun():\n"
105  << "set_digital_out(" << (int)pin << "," << (value ? "True" : "False") << ")\n"
106  << "end\n";
107  std::string s(out.str());
108  return write(s);
109 }
110 
111 bool URCommander_V3_X::setAnalogOut(uint8_t pin, double value)
112 {
113  std::ostringstream out;
114  out << "sec io_fun():\n"
115  << "set_standard_analog_out(" << (int)pin << "," << std::fixed << std::setprecision(5) << value << ")\n"
116  << "end\n";
117  std::string s(out.str());
118  return write(s);
119 }
120 
121 bool URCommander_V3_X::setDigitalOut(uint8_t pin, bool value)
122 {
123  std::ostringstream out;
124  std::string func;
125 
126  if (pin < 8)
127  {
128  func = "set_standard_digital_out";
129  }
130  else if (pin < 16)
131  {
132  func = "set_configurable_digital_out";
133  pin -= 8;
134  }
135  else if (pin < 18)
136  {
137  func = "set_tool_digital_out";
138  pin -= 16;
139  }
140  else
141  return false;
142 
143  out << "sec io_fun():\n"
144  << func << "(" << (int)pin << "," << (value ? "True" : "False") << ")\n"
145  << "end\n";
146  std::string s(out.str());
147  return write(s);
148 }
149 
150 bool URCommander_V3_1__2::speedj(std::array<double, 6> &speeds, double acceleration)
151 {
152  std::ostringstream out;
153  out << std::fixed << std::setprecision(5);
154  out << "speedj(";
155  formatArray(out, speeds);
156  out << "," << acceleration << ")\n";
157  std::string s(out.str());
158  return write(s);
159 }
160 
161 bool URCommander_V3_3::speedj(std::array<double, 6> &speeds, double acceleration)
162 {
163  std::ostringstream out;
164  out << std::fixed << std::setprecision(5);
165  out << "speedj(";
166  formatArray(out, speeds);
167  out << "," << acceleration << "," << 0.008 << ")\n";
168  std::string s(out.str());
169  return write(s);
170 }
virtual bool setDigitalOut(uint8_t pin, bool value)
Definition: commander.cpp:101
virtual bool setAnalogOut(uint8_t pin, double value)
Definition: commander.cpp:91
void formatArray(std::ostringstream &out, std::array< double, 6 > &values)
Definition: commander.cpp:30
bool uploadProg(const std::string &s)
Definition: commander.cpp:41
XmlRpcServer s
URStream & stream_
Definition: commander.h:28
#define LOG_DEBUG(format,...)
Definition: log.h:33
bool setToolVoltage(uint8_t voltage)
Definition: commander.cpp:47
virtual bool speedj(std::array< double, 6 > &speeds, double acceleration)
Definition: commander.cpp:150
virtual bool setAnalogOut(uint8_t pin, double value)
Definition: commander.cpp:111
bool setPayload(double value)
Definition: commander.cpp:65
bool stopj(double a=10.0)
Definition: commander.cpp:73
bool write(const std::string &s)
Definition: commander.cpp:22
bool setFlag(uint8_t pin, bool value)
Definition: commander.cpp:58
virtual bool speedj(std::array< double, 6 > &speeds, double acceleration)
Definition: commander.cpp:161
virtual bool setDigitalOut(uint8_t pin, bool value)
Definition: commander.cpp:121
bool write(const uint8_t *buf, size_t buf_len, size_t &written)
Definition: stream.cpp:27
virtual bool speedj(std::array< double, 6 > &speeds, double acceleration)
Definition: commander.cpp:81


ur_modern_driver
Author(s): Thomas Timm Andersen, Simon Rasmussen
autogenerated on Fri Jun 26 2020 03:37:00