Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
IselLinearControl.cpp
1 #include "IselLinearControl.hh"
2 #include <Base/Common/W32Compat.hh>
3 #include <Base/Common/BIASpragma.hh>
4 #include <Base/Debug/Error.hh>
5 #include <cstring>
6 #include <sstream>
7 
8 using namespace std;
9 using namespace BIAS;
10 
11 IselLinearControl::IselLinearControl()
12 {
13  speed_ = 40000;
14  steps_per_mm_ = 640; // constant for our isel control
15 }
16 
17 IselLinearControl::~IselLinearControl() {}
18 
19 
20 int IselLinearControl::Reset()
21 {
22  SendRawCommand("@0R1\r");
23  return CheckStatus();
24 }
25 
26 int IselLinearControl::Init(int nCOMportNumber)
27 {
28  char COMportPrefix[10] = "/dev/ttyS";
29  char COMportName[17];
30  sprintf(COMportName, "%s%d", COMportPrefix, nCOMportNumber);
31  return Init(COMportName);
32 }
33 
34 int IselLinearControl::Init(const char *COMportName)
35 {
36  cout << "YoYoYo" << endl;
37  cout << "Opening port " << COMportName << " for Isel Linear."<<endl;
38  COMstream_ = open_host_port((char*)COMportName);
39  if (COMstream_ == PORT_NOT_OPENED) {
40  return -1;
41  }
42  SendRawCommand("@01\r");
43  return CheckStatus();
44 }
45 
46 void IselLinearControl::Close()
47 {
48  close_host_port(COMstream_);
49 }
50 
51 
52 int IselLinearControl::SetSpeed(int speed)
53 {
54  if (speed > 0 && speed <= 40000) {
55  speed_ = speed;
56  return 0;
57  } else {
58  BIASERR("speed out of bounds, use value 0..40000");
59  return -1;
60  }
61 }
62 
63 void IselLinearControl::GetSpeed(int &speed)
64 {
65  speed = speed_;
66 }
67 
68 int IselLinearControl::SetAcceleration(int accel)
69 {
70  /* TODO
71  stringstream s;
72  s << "@0_" << accel << "\r";
73  SendRawCommand(s.str());
74  return CheckStatus();
75  */
76  BIASERR("implement me");
77  return 0;
78 }
79 
80 void IselLinearControl::GetAcceleration(int &accel)
81 {
82  // TODO
83  BIASERR("implement me");
84 }
85 
86 int IselLinearControl::MoveLeft(float millimeters)
87 {
88  float steps = millimeters * steps_per_mm_;
89  int nValue = (int)steps;
90  if (steps - (float)nValue > 0.0f) {
91  BIASWARN("\nwant to move " << steps << " steps. flooring to " << nValue << " steps, loosing precision.\n"
92  << "Isel control does 1/"<<steps_per_mm_<<" steps.");
93  }
94  stringstream s;
95  s << "@0A" << nValue << "," << speed_ << "\r";
96  SendRawCommand(s.str());
97  return CheckStatus();
98 }
99 
100 int IselLinearControl::MoveRight(float millimeters)
101 {
102  float steps = millimeters * steps_per_mm_;
103  int nValue = (int)steps;
104  if (steps - (float)nValue > 0.0f) {
105  BIASWARN("\nwant to move " << steps << " steps. flooring to " << nValue << " steps, loosing precision.\n"
106  << "Isel control does 1/"<<steps_per_mm_<<" steps.");
107  }
108  nValue = -nValue;
109  stringstream s;
110  s << "@0A" << nValue << "," << speed_ << "\r";
111  SendRawCommand(s.str());
112  return CheckStatus();
113 }
114 
115 void IselLinearControl::GetCurrentPosition(int &pos)
116 {
117  // TODO
118  BIASERR("implement me");
119 }
120 
121 int IselLinearControl::SetPosition(int pos, bool bWaitComplete)
122 {
123  stringstream s;
124  s << "@0M" << pos << "," << speed_ << "\r";
125  SendRawCommand(s.str());
126  if (bWaitComplete) {
127  return CheckStatus();
128  }
129  return 0;
130 }
131 
132 void IselLinearControl::SendRawCommand(string cmd) {
133  SerialStringOut(COMstream_, (unsigned char*) cmd.c_str());
134 }
135 
136 int IselLinearControl::ReadRawLine(string &cmd) {
137  char result[0xFF];
138  int len = 0;
139  ReadSerialLine(COMstream_, (unsigned char*)result,1,&len);
140  cmd = string(result,len);
141  return len;
142 }
143 
144 int IselLinearControl::CheckStatus() {
145  string test;
146  while (ReadRawLine(test) == 0) {}
147  if (test.length() == 1) {
148  unsigned char c = test.c_str()[0];
149  switch (c) {
150  case '0':
151  // no error
152  return 0;
153  case '2':
154  BIASERR("reached limit, need to move free");
155  SendRawCommand("@0F1\r");
156  return CheckStatus();
157  default:
158  BIASERR("Got Errorcode: " << test);
159  return -1;
160  }
161  }
162  BIASERR("unknown response: " << test);
163  return -1;
164 }