1 #include "IselLinearControlTwoAxis.hh"
2 #include <Base/Common/W32Compat.hh>
3 #include <Base/Common/BIASpragma.hh>
4 #include <Base/Debug/Error.hh>
14 IselLinearControlTwoAxis::IselLinearControlTwoAxis()
16 steps_per_second_x_ = 40000;
17 steps_per_second_y_ = 40000;
19 steps_per_mm_x_ = 160;
20 steps_per_mm_y_ = 320;
23 IselLinearControlTwoAxis::IselLinearControlTwoAxis(
const int steps_per_mm_x,
const int steps_per_mm_y)
25 steps_per_mm_x_ = steps_per_mm_x;
26 steps_per_mm_y_ = steps_per_mm_y;
29 IselLinearControlTwoAxis::~IselLinearControlTwoAxis() {}
31 int IselLinearControlTwoAxis::Reset(
const int axis)
34 cmd <<
"@0R" << axis <<
"\r";
37 if((axis > 0)&& (axis<4)){
38 SendRawCommand(cmd.str());
45 int IselLinearControlTwoAxis::Init(
const int nCOMportNumber)
47 char COMportPrefix[10] =
"/dev/ttyS";
49 sprintf(COMportName,
"%s%d", COMportPrefix, nCOMportNumber);
50 return Init(COMportName);
53 int IselLinearControlTwoAxis::Init(
const char *COMportName)
55 cout <<
"Opening port " << COMportName <<
" for Isel Linear."<<endl;
56 COMstream_ = open_host_port((
char*)COMportName);
57 if (COMstream_ == PORT_NOT_OPENED) {
65 info.c_iflag = IGNBRK | IGNPAR;
66 info.c_iflag &= ~(INLCR | ICRNL | IUCLC | ISTRIP | IXON | BRKINT);
67 info.c_oflag &= ~OPOST;
68 info.c_lflag &= ~(ICANON | ISIG | ECHO);
69 info.c_cflag = B19200 | CS8 | CREAD | CLOCAL;
71 if (tcsetattr(COMstream_, TCSANOW, &info) < 0)
73 cout <<
"Error using TCSETS in ioctl" << endl;
74 close_host_port(COMstream_);
79 SendRawCommand(
"@03\r");
83 void IselLinearControlTwoAxis::Close()
85 close_host_port(COMstream_);
89 int IselLinearControlTwoAxis::SetSpeed(
const float speedX,
const float speedY)
91 int tmp_speed_in_steps_x = (int) speedX * steps_per_mm_x_;
92 int tmp_speed_in_steps_y = (int) speedY * steps_per_mm_y_;
94 if (tmp_speed_in_steps_x > 0 && tmp_speed_in_steps_x <= 40000 &&
95 tmp_speed_in_steps_y > 0 && tmp_speed_in_steps_y <= 40000) {
96 steps_per_second_x_ = tmp_speed_in_steps_x;
97 steps_per_second_y_ = tmp_speed_in_steps_y;
101 tmp <<
"speed out of bounds, use value 1..40000 steps per second (" << (1/steps_per_mm_x_) <<
" < x < "<< (40000/steps_per_mm_x_) <<
", " << (1/steps_per_mm_y_) <<
" < y < "<< (40000/steps_per_mm_y_) <<
" [mm/s])";
107 void IselLinearControlTwoAxis::GetSpeed(
float &speedX,
float &speedY)
109 speedX = steps_per_second_x_ / steps_per_mm_x_;
110 speedY = steps_per_second_y_ / steps_per_mm_y_;
113 int IselLinearControlTwoAxis::SetAcceleration(
const int accel)
121 BIASERR(
"implement me");
125 void IselLinearControlTwoAxis::GetAcceleration(
int &accel)
128 BIASERR(
"implement me");
131 int IselLinearControlTwoAxis::Move(
const float millimetersX,
const float millimetersY)
133 float stepsX, stepsY;
134 stepsX = millimetersX * steps_per_mm_x_;
135 stepsY = millimetersY * steps_per_mm_y_;
137 int nValueX = (int)stepsX;
138 int nValueY = (int)stepsY;
140 if (stepsX - (
float)nValueX > 0.0f) {
141 BIASWARN(
"\nwant to move " << stepsX <<
" steps. flooring to " << nValueX <<
" steps, loosing precision.\n");
144 if (stepsY - (
float)nValueY > 0.0f) {
145 BIASWARN(
"\nwant to move " << stepsY <<
" steps. flooring to " << nValueY <<
" steps, loosing precision.\n");
149 s <<
"@0A" << nValueX <<
"," << steps_per_second_x_ <<
"," << nValueY <<
"," << steps_per_second_y_ <<
"\r";
150 SendRawCommand(s.str());
151 return CheckStatus();
154 void IselLinearControlTwoAxis::GetCurrentPosition(
float &millimetersX,
float &millimetersY)
159 SendRawCommand(
"@0P\r");
161 size = ReadRawLine(output);
165 if(output.c_str()[size-18] >=
'8')
169 ss << output.substr(size-18,6);
171 millimetersX = (float) i / (
float) steps_per_mm_x_;
174 if(output.c_str()[size-12] >=
'8')
178 ss << output.substr(size-12,6);
180 millimetersY = (float) i / (
float) steps_per_mm_y_;
182 BIASWARN(
"\ncant determine position! \n");
186 int IselLinearControlTwoAxis::SetPosition(
const float millimetersX,
const float millimetersY,
const bool bWaitComplete)
188 float posX = millimetersX * (float) steps_per_mm_x_;
189 float posY = millimetersY * (float) steps_per_mm_y_;
191 int nValueX = (int)posX;
192 int nValueY = (int)posY;
194 if (posX - (
float)nValueX > 0.0f) {
195 BIASWARN(
"\nwant to move to position " << posX <<
" in steps on x-axis. flooring to position " << nValueX <<
" in steps, loosing precision.\n");
198 if (posY - (
float)nValueY > 0.0f) {
199 BIASWARN(
"\nwant to move to position " << posY <<
" in steps on y-axis. flooring to position " << nValueY <<
" in steps, loosing precision.\n");
203 s <<
"@0M" << nValueX <<
"," << steps_per_second_x_ <<
"," << nValueY <<
"," << steps_per_second_y_ <<
"\r";
204 SendRawCommand(s.str());
207 return CheckStatus();
212 void IselLinearControlTwoAxis::SendRawCommand(
const string cmd) {
213 SerialStringOut(COMstream_, (
unsigned char*) cmd.c_str());
217 int IselLinearControlTwoAxis::ReadRawLine(
string &cmd) {
220 ReadSerialLine(COMstream_, (
unsigned char*)result,1,&len);
221 cmd = string(result,len);
225 int IselLinearControlTwoAxis::CheckStatus() {
228 while (ReadRawLine(test) == 0) {
230 while (i < test.size() && test.c_str()[i] ==
'0') {
233 if (test.size() == i) {
237 unsigned char c = test.c_str()[i];
240 BIASERR(
"reached limit - move free and reset");
241 SendRawCommand(
"@0F3\r");
242 return (CheckStatus() == 0 ? Reset() : -1);
244 BIASERR(
"possible emergency stop - reinit and reset");
245 SendRawCommand(
"@03\r");
246 return (CheckStatus() == 0 ? Reset() : -1);
248 BIASERR(
"speed out of bounds");
251 BIASERR(
"need to reset");
254 BIASERR(
"Got Errorcode: " << test);
258 BIASERR(
"unknown response: " << test);