| 1 | #include "macs.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <iostream.h>
|
|---|
| 4 | #include <sys/time.h> // timeval->tv_sec
|
|---|
| 5 |
|
|---|
| 6 | #include "timer.h"
|
|---|
| 7 | #include "network.h"
|
|---|
| 8 | #include "MLogManip.h"
|
|---|
| 9 |
|
|---|
| 10 | ClassImp(Macs);
|
|---|
| 11 |
|
|---|
| 12 | /*
|
|---|
| 13 | ---------------------------
|
|---|
| 14 | For test purposes
|
|---|
| 15 | ---------------------------
|
|---|
| 16 |
|
|---|
| 17 | class MyTimer : public TTimer
|
|---|
| 18 | {
|
|---|
| 19 | public:
|
|---|
| 20 | MyTimer(TObject *obj, Long_t ms, Bool_t mode) : TTimer(obj, ms, mode) {}
|
|---|
| 21 | Bool_t Notify()
|
|---|
| 22 | {
|
|---|
| 23 | cout << "Notify" << endl;
|
|---|
| 24 | TTimer::Notify();
|
|---|
| 25 | return kTRUE;
|
|---|
| 26 | }
|
|---|
| 27 | };
|
|---|
| 28 | */
|
|---|
| 29 |
|
|---|
| 30 | Macs::Macs(const BYTE_t nodeid, const char *name, MLog &out)
|
|---|
| 31 | : NodeDrv(nodeid, name, out), fMacId(2*nodeid+1),
|
|---|
| 32 | fPos(0), fPosTime(0.0), fPdoPos(0), fPdoTime(0.0),
|
|---|
| 33 | fPosActive(0), fRpmActive(0), fStatusPdo3(0xff)
|
|---|
| 34 | {
|
|---|
| 35 | // fTimeout = new TTimer(this, 100); //, kFALSE); // 100ms, asynchronous
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | Macs::~Macs()
|
|---|
| 39 | {
|
|---|
| 40 | //fTimerOn = kFALSE;
|
|---|
| 41 | // delete fTimeout;
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | void Macs::HandleSDO(WORD_t idx, BYTE_t subidx, LWORD_t val, timeval_t *tv)
|
|---|
| 45 | {
|
|---|
| 46 | // cout << "SdoRx: Idx=0x"<< hex << idx << "/" << (int)subidx;
|
|---|
| 47 | // cout << ", val=0x" << val << endl;
|
|---|
| 48 | switch (idx)
|
|---|
| 49 | {
|
|---|
| 50 | case 0x1003:
|
|---|
| 51 | // FIXME, see Init
|
|---|
| 52 | if (subidx!=2)
|
|---|
| 53 | return;
|
|---|
| 54 | lout << "- " << GetNodeName() << ": Error[0]=" << dec << val << endl;
|
|---|
| 55 | SetError(val);
|
|---|
| 56 | return;
|
|---|
| 57 |
|
|---|
| 58 | case 0x100a:
|
|---|
| 59 | lout << "- " << GetNodeName() << ": Using Software Version V" << dec << (int)(val>>16) << "." << (int)(val&0xff) << endl;
|
|---|
| 60 | fSoftVersion = val;
|
|---|
| 61 | return;
|
|---|
| 62 |
|
|---|
| 63 | case 0x100b:
|
|---|
| 64 | // Do not display, this is used for CheckConnection
|
|---|
| 65 | // lout << "Node ID: " << dec << val << endl;
|
|---|
| 66 | return;
|
|---|
| 67 |
|
|---|
| 68 | case 0x2002:
|
|---|
| 69 | cout << GetNodeName() << ": Current velocity: " << dec << val << endl;
|
|---|
| 70 | fVel = val;
|
|---|
| 71 | return;
|
|---|
| 72 |
|
|---|
| 73 | case 0x6004:
|
|---|
| 74 | if (subidx)
|
|---|
| 75 | return;
|
|---|
| 76 |
|
|---|
| 77 | // lout << "Actual Position: " << dec << (signed long)val << endl;
|
|---|
| 78 | fPos = (LWORDS_t)val;
|
|---|
| 79 | fPosTime.SetTimer(tv);
|
|---|
| 80 | return;
|
|---|
| 81 | /*
|
|---|
| 82 | case 0x2001:
|
|---|
| 83 | cout << "Axe Status: 0x" << hex << val << endl;
|
|---|
| 84 | cout << " - Motor " << (val&0x01?"standing":"moving") << endl;
|
|---|
| 85 | cout << " - Positioning " << (val&0x02?"active":"inactive") << endl;
|
|---|
| 86 | cout << " - Rotary mode " << (val&0x04?"active":"inactive") << endl;
|
|---|
| 87 | cout << " - Attitude control: " << (val&0x40?"off":"on") << endl;
|
|---|
| 88 | cout << " - Axe resetted: " << (val&0x80?"yes":"no") << endl;
|
|---|
| 89 | fPosActive = val&0x02;
|
|---|
| 90 | fRpmActive = val&0x04;
|
|---|
| 91 | return;
|
|---|
| 92 | case 0x2003:
|
|---|
| 93 | if (!subidx)
|
|---|
| 94 | {
|
|---|
| 95 | cout << "Input State: ";
|
|---|
| 96 | for (int i=0; i<8; i++)
|
|---|
| 97 | cout << (int)(val&(1<<i)?1:0);
|
|---|
| 98 | cout <<endl;
|
|---|
| 99 | return;
|
|---|
| 100 | }
|
|---|
| 101 | cout << "Input No." << subidx << (val?"hi":"lo") << endl;
|
|---|
| 102 | return;
|
|---|
| 103 | case 0x2004:
|
|---|
| 104 | cout << "Status Value of Axis: 0x" << hex << val << endl;
|
|---|
| 105 | cout << " - Attitude control: " << (val&0x800000?"off":"on") << endl;
|
|---|
| 106 | cout << " - Movement done: " << (val&0x040000?"yes":"no") << endl;
|
|---|
| 107 | cout << " - Number of Control Units: " << (int)((val>>8)&0xff) << endl;
|
|---|
| 108 | cout << " - Attitude control: " << (val&0x04?"off":"on") << endl;
|
|---|
| 109 | cout << " - Startswitch active: " << (val&0x20?"yes":"no") << endl;
|
|---|
| 110 | cout << " - Referenceswitch active: " << (val&0x40?"yes":"no") << endl;
|
|---|
| 111 | cout << " - Endswitch active: " << (val&0x80?"yes":"no") << endl;
|
|---|
| 112 | return;
|
|---|
| 113 | */
|
|---|
| 114 |
|
|---|
| 115 | case 0x6002:
|
|---|
| 116 | lout << "- " << GetNodeName() << ": Velocity resolution = " << dec << val << " ticks/min" << endl;
|
|---|
| 117 | fVelRes = val;
|
|---|
| 118 | return;
|
|---|
| 119 |
|
|---|
| 120 | case 0x6501:
|
|---|
| 121 | lout << "- " << GetNodeName() << ": Encoder resolution = " << dec << val << " ticks/min" << endl;
|
|---|
| 122 | fRes = val;
|
|---|
| 123 | return;
|
|---|
| 124 | }
|
|---|
| 125 | cout << "Macs: SDO, idx=0x"<< hex << idx << "/" << (int)subidx;
|
|---|
| 126 | cout << ", val=0x"<<val<<endl;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | void Macs::HandleSDOOK(WORD_t idx, BYTE_t subidx, timeval_t *tv)
|
|---|
| 130 | {
|
|---|
| 131 | // cout << "Node #" << dec << (int)GetId() << ": Sdo=" << hex << idx << "/" << (int)subidx << " set." << endl;
|
|---|
| 132 | switch (idx)
|
|---|
| 133 | {
|
|---|
| 134 | case 0x2002:
|
|---|
| 135 | switch (subidx)
|
|---|
| 136 | {
|
|---|
| 137 | case 0:
|
|---|
| 138 | //lout << ddev(MLog::eGui);
|
|---|
| 139 | lout << "- " << GetNodeName() << ": Velocity set." << endl;
|
|---|
| 140 | //lout << edev(MLog::eGui);
|
|---|
| 141 | return;
|
|---|
| 142 | }
|
|---|
| 143 | break;
|
|---|
| 144 |
|
|---|
| 145 | case 0x2003:
|
|---|
| 146 | switch (subidx)
|
|---|
| 147 | {
|
|---|
| 148 | case 0:
|
|---|
| 149 | //lout << ddev(MLog::eGui);
|
|---|
| 150 | lout << "- " << GetNodeName() << ": Acceleration set." << endl;
|
|---|
| 151 | //lout << edev(MLog::eGui);
|
|---|
| 152 | return;
|
|---|
| 153 | case 1:
|
|---|
| 154 | //lout << ddev(MLog::eGui);
|
|---|
| 155 | lout << "- " << GetNodeName() << ": Deceleration set." << endl;
|
|---|
| 156 | //lout << edev(MLog::eGui);
|
|---|
| 157 | return;
|
|---|
| 158 | }
|
|---|
| 159 | break;
|
|---|
| 160 |
|
|---|
| 161 | case 0x3006:
|
|---|
| 162 | switch (subidx)
|
|---|
| 163 | {
|
|---|
| 164 | case 0:
|
|---|
| 165 | //lout << ddev(MLog::eGui);
|
|---|
| 166 | lout << "- " << GetNodeName() << ": RPM mode switched." << endl;
|
|---|
| 167 | //lout << edev(MLog::eGui);
|
|---|
| 168 | return;
|
|---|
| 169 |
|
|---|
| 170 | case 1:
|
|---|
| 171 | /*
|
|---|
| 172 | lout << ddev(MLog::eGui);
|
|---|
| 173 | lout << "- Velocity set (" << GetNodeName() << ")" << endl;
|
|---|
| 174 | lout << edev(MLog::eGui);
|
|---|
| 175 | */
|
|---|
| 176 | return;
|
|---|
| 177 | }
|
|---|
| 178 | break;
|
|---|
| 179 |
|
|---|
| 180 | case 0x4000:
|
|---|
| 181 | HandleNodeguard(tv);
|
|---|
| 182 | return;
|
|---|
| 183 |
|
|---|
| 184 | case 0x6004:
|
|---|
| 185 | switch (subidx)
|
|---|
| 186 | {
|
|---|
| 187 | case 0:
|
|---|
| 188 | //lout << ddev(MLog::eGui);
|
|---|
| 189 | lout << "- " << GetNodeName() << ": Absolute positioning started." << endl;
|
|---|
| 190 | //lout << edev(MLog::eGui);
|
|---|
| 191 | return;
|
|---|
| 192 |
|
|---|
| 193 | case 1:
|
|---|
| 194 | //lout << ddev(MLog::eGui);
|
|---|
| 195 | lout << "- " << GetNodeName() << ": Relative positioning started." << endl;
|
|---|
| 196 | //lout << edev(MLog::eGui);
|
|---|
| 197 | return;
|
|---|
| 198 | }
|
|---|
| 199 | break;
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 | }
|
|---|
| 203 | NodeDrv::HandleSDOOK(idx, subidx, tv);
|
|---|
| 204 | }
|
|---|
| 205 |
|
|---|
| 206 | void Macs::ReqVelRes()
|
|---|
| 207 | {
|
|---|
| 208 | lout << "- " << GetNodeName() << ": Requesting velocity resolution (velres, 0x6002)." << endl;
|
|---|
| 209 | RequestSDO(0x6002);
|
|---|
| 210 | WaitForSdo(0x6002);
|
|---|
| 211 | }
|
|---|
| 212 |
|
|---|
| 213 | void Macs::ReqRes()
|
|---|
| 214 | {
|
|---|
| 215 | lout << "- " << GetNodeName() << ": Requesting encoder resolution (res, 0x6501)." << endl;
|
|---|
| 216 | RequestSDO(0x6501);
|
|---|
| 217 | WaitForSdo(0x6501);
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | void Macs::SetPDO1On(BYTE_t flag)
|
|---|
| 221 | {
|
|---|
| 222 | lout << "- " << GetNodeName() << ": " << (flag?"Enable":"Disable") << " PDO1." << endl;
|
|---|
| 223 | SendSDO(0x1800, 1, (LWORD_t)(flag?0:1)<<31);
|
|---|
| 224 | WaitForSdo(0x1800, 1);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | void Macs::CheckConnection()
|
|---|
| 228 | {
|
|---|
| 229 | RequestSDO(0x100b);
|
|---|
| 230 | WaitForSdo(0x100b);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | void Macs::Init()
|
|---|
| 234 | {
|
|---|
| 235 | //
|
|---|
| 236 | // Request current error status (FIXME: is the first entry in the
|
|---|
| 237 | // error list)
|
|---|
| 238 | //
|
|---|
| 239 | lout << "- " << GetNodeName() << ": Requesting Error[0]." << endl;
|
|---|
| 240 | RequestSDO(0x1003, 2);
|
|---|
| 241 | WaitForSdo(0x1003, 2);
|
|---|
| 242 | if (HasError())
|
|---|
| 243 | {
|
|---|
| 244 | lout << "Macs::Init: " << GetNodeName() << " has error --> ZOMBIE!" << endl;
|
|---|
| 245 | SetZombie();
|
|---|
| 246 | }
|
|---|
| 247 | if (IsZombieNode())
|
|---|
| 248 | return;
|
|---|
| 249 |
|
|---|
| 250 | StopHostGuarding();
|
|---|
| 251 | StopGuarding();
|
|---|
| 252 |
|
|---|
| 253 | usleep(2000*GetGuardTime());
|
|---|
| 254 |
|
|---|
| 255 | lout << "- " << GetNodeName() << ": Requesting Mac Software Version." << endl;
|
|---|
| 256 | RequestSDO(0x100a);
|
|---|
| 257 | WaitForSdo(0x100a);
|
|---|
| 258 | // FIXME! Not statically linked!
|
|---|
| 259 | if (fSoftVersion<0x00000044) // 00.68
|
|---|
| 260 | {
|
|---|
| 261 | lout << GetNodeName() << " - Software Version too old!" << endl;
|
|---|
| 262 | SetZombie();
|
|---|
| 263 | return;
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | SetRpmMode(FALSE);
|
|---|
| 267 |
|
|---|
| 268 | ReqRes(); // Init fRes
|
|---|
| 269 | ReqVelRes(); // Init fVelRes
|
|---|
| 270 |
|
|---|
| 271 | lout << "- " << GetNodeName() << ": Motor on." << endl;
|
|---|
| 272 | SendSDO(0x3000, string('o', 'n'));
|
|---|
| 273 | WaitForSdo(0x3000);
|
|---|
| 274 |
|
|---|
| 275 | SetPDO1On(FALSE); // this is a workaround for the Macs
|
|---|
| 276 | SetPDO1On(TRUE);
|
|---|
| 277 |
|
|---|
| 278 | SetNoWait(TRUE);
|
|---|
| 279 |
|
|---|
| 280 | // StartGuarding(200, 2, kFALSE); // Using PDO1 @ 100ms
|
|---|
| 281 |
|
|---|
| 282 | // StartGuarding(250, 4);
|
|---|
| 283 | // StartHostGuarding();
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | void Macs::StopMotor()
|
|---|
| 287 | {
|
|---|
| 288 | //
|
|---|
| 289 | // Stop the motor and switch off the position control unit
|
|---|
| 290 | //
|
|---|
| 291 | SendSDO(0x3000, string('s','t','o','p'));
|
|---|
| 292 | WaitForSdo(0x3000);
|
|---|
| 293 | }
|
|---|
| 294 |
|
|---|
| 295 | void Macs::StopDevice()
|
|---|
| 296 | {
|
|---|
| 297 | //EnableTimeout(kFALSE);
|
|---|
| 298 |
|
|---|
| 299 | SetNoWait(FALSE);
|
|---|
| 300 |
|
|---|
| 301 | StopHostGuarding();
|
|---|
| 302 | StopGuarding();
|
|---|
| 303 |
|
|---|
| 304 | //
|
|---|
| 305 | // FIXME: This isn't called if the initialization isn't done completely!
|
|---|
| 306 | //
|
|---|
| 307 |
|
|---|
| 308 | SetRpmMode(FALSE);
|
|---|
| 309 |
|
|---|
| 310 | SetPDO1On(FALSE);
|
|---|
| 311 |
|
|---|
| 312 | /*
|
|---|
| 313 | lout << "- " << GetNodeName() << ": Motor off." << endl;
|
|---|
| 314 | SendSDO(0x3000, string('o', 'f', 'f'));
|
|---|
| 315 | WaitForSdo(0x3000);
|
|---|
| 316 | */
|
|---|
| 317 |
|
|---|
| 318 | /*
|
|---|
| 319 | lout << "- Stopping Program of " << (int)GetId() << endl;
|
|---|
| 320 | SendSDO(0x4000, (LWORD_t)0xaffe);
|
|---|
| 321 | WaitForSdo();
|
|---|
| 322 | */
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | void Macs::ReqPos()
|
|---|
| 326 | {
|
|---|
| 327 | lout << "- " << GetNodeName() << ": Requesting Position." << endl;
|
|---|
| 328 | RequestSDO(0x6004);
|
|---|
| 329 | WaitForSdo(0x6004);
|
|---|
| 330 | }
|
|---|
| 331 |
|
|---|
| 332 | void Macs::ReqVel()
|
|---|
| 333 | {
|
|---|
| 334 | lout << "- " << GetNodeName() << ": Requesting Velocity." << endl;
|
|---|
| 335 | RequestSDO(0x2002);
|
|---|
| 336 | WaitForSdo(0x2002);
|
|---|
| 337 | }
|
|---|
| 338 |
|
|---|
| 339 | void Macs::SetHome(LWORDS_t pos, WORD_t maxtime)
|
|---|
| 340 | {
|
|---|
| 341 | StopHostGuarding();
|
|---|
| 342 | StopGuarding();
|
|---|
| 343 |
|
|---|
| 344 | lout << "- " << GetNodeName() << ": Driving to home position, Offset=" << dec << pos << endl;
|
|---|
| 345 | SendSDO(0x6003, 2, (LWORD_t)pos); // home
|
|---|
| 346 | WaitForSdo(0x6003, 2);
|
|---|
| 347 |
|
|---|
| 348 | // home also defines the zero point of the system
|
|---|
| 349 | // maximum time allowd for home drive: 25.000ms
|
|---|
| 350 | SendSDO(0x3001, string('h','o','m','e')); // home
|
|---|
| 351 | WaitForSdo(0x3001, 0, maxtime*1000);
|
|---|
| 352 | lout << "- " << GetNodeName() << ": Home position reached. " << endl;
|
|---|
| 353 |
|
|---|
| 354 | SendSDO(0x6003, 0, string('s','e','t')); // home
|
|---|
| 355 | WaitForSdo(0x6003, 0);
|
|---|
| 356 |
|
|---|
| 357 | StartGuarding();
|
|---|
| 358 | StartHostGuarding();
|
|---|
| 359 | }
|
|---|
| 360 |
|
|---|
| 361 | void Macs::SetVelocity(LWORD_t vel)
|
|---|
| 362 | {
|
|---|
| 363 | SendSDO(0x2002, vel); // velocity
|
|---|
| 364 | WaitForSdo(0x2002, 0);
|
|---|
| 365 | }
|
|---|
| 366 |
|
|---|
| 367 | void Macs::SetAcceleration(LWORD_t acc)
|
|---|
| 368 | {
|
|---|
| 369 | SendSDO(0x2003, 0, acc); // acceleration
|
|---|
| 370 | WaitForSdo(0x2003, 0);
|
|---|
| 371 | }
|
|---|
| 372 |
|
|---|
| 373 | void Macs::SetDeceleration(LWORD_t dec)
|
|---|
| 374 | {
|
|---|
| 375 | SendSDO(0x2003, 1, dec); // acceleration
|
|---|
| 376 | WaitForSdo(0x2003, 1);
|
|---|
| 377 | }
|
|---|
| 378 |
|
|---|
| 379 | void Macs::SetRpmMode(BYTE_t mode)
|
|---|
| 380 | {
|
|---|
| 381 | //
|
|---|
| 382 | // SetRpmMode(FALSE) stop the motor, but lets the position control unit on
|
|---|
| 383 | //
|
|---|
| 384 | SendSDO(0x3006, 0, mode ? string('s','t','r','t') : string('s','t','o','p'));
|
|---|
| 385 | WaitForSdo(0x3006, 0);
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | void Macs::SetRpmVelocity(LWORDS_t cvel)
|
|---|
| 389 | {
|
|---|
| 390 | SendSDO(0x3006, 1, (LWORD_t)cvel);
|
|---|
| 391 | WaitForSdo(0x3006, 1);
|
|---|
| 392 | }
|
|---|
| 393 |
|
|---|
| 394 | void Macs::StartRelPos(LWORDS_t pos)
|
|---|
| 395 | {
|
|---|
| 396 | SendSDO(0x6004, 1, (LWORD_t)pos);
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | void Macs::StartAbsPos(LWORDS_t pos)
|
|---|
| 400 | {
|
|---|
| 401 | SendSDO(0x6004, 0, (LWORD_t)pos);
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | void Macs::SetNoWait(BYTE_t flag)
|
|---|
| 405 | {
|
|---|
| 406 | lout << "- " << GetNodeName() << ": Setting NOWAIT " << (flag?"ON":"OFF") << "." << endl;
|
|---|
| 407 | SendSDO(0x3008, flag ? string('o', 'n') : string('o', 'f', 'f'));
|
|---|
| 408 | WaitForSdo(0x3008);
|
|---|
| 409 | }
|
|---|
| 410 |
|
|---|
| 411 | void Macs::StartVelSync()
|
|---|
| 412 | {
|
|---|
| 413 | //
|
|---|
| 414 | // The syncronization mode is disabled by a 'MOTOR STOP'
|
|---|
| 415 | // or by a positioning command (POSA, ...)
|
|---|
| 416 | //
|
|---|
| 417 | lout << "- " << GetNodeName() << ": Starting RPM Sync Mode." << endl;
|
|---|
| 418 | SendSDO(0x3007, 0, string('s', 'y', 'n', 'c'));
|
|---|
| 419 | WaitForSdo(0x3007, 0);
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | void Macs::StartPosSync()
|
|---|
| 423 | {
|
|---|
| 424 | //
|
|---|
| 425 | // The syncronization mode is disabled by a 'MOTOR STOP'
|
|---|
| 426 | // or by a positioning command (POSA, ...)
|
|---|
| 427 | //
|
|---|
| 428 | lout << "- " << GetNodeName() << ": Starting Position Sync Mode." << endl;
|
|---|
| 429 | SendSDO(0x3007, 1, string('s', 'y', 'n', 'c'));
|
|---|
| 430 | WaitForSdo(0x3007, 1);
|
|---|
| 431 | }
|
|---|
| 432 | /*
|
|---|
| 433 | void Macs::ReqAxEnd()
|
|---|
| 434 | {
|
|---|
| 435 | RequestSDO(0x2001);
|
|---|
| 436 | WaitForSdo(0x2001);
|
|---|
| 437 | }
|
|---|
| 438 | */
|
|---|
| 439 | void Macs::SendMsg(BYTE_t data[6])
|
|---|
| 440 | {
|
|---|
| 441 | GetNetwork()->SendCanFrame(fMacId, 0, 0, data[0], data[1], data[2], data[3], data[4], data[5]);
|
|---|
| 442 | }
|
|---|
| 443 |
|
|---|
| 444 | void Macs::SendMsg(BYTE_t d0=0, BYTE_t d1=0, BYTE_t d2=0,
|
|---|
| 445 | BYTE_t d3=0, BYTE_t d4=0, BYTE_t d5=0)
|
|---|
| 446 | {
|
|---|
| 447 | GetNetwork()->SendCanFrame(fMacId, 0, 0, d0, d1, d2, d3, d4, d5);
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | void Macs::HandlePDO1(BYTE_t *data, timeval_t *tv)
|
|---|
| 451 | {
|
|---|
| 452 | // FIXME!!!!
|
|---|
| 453 | HandleNodeguard(tv);
|
|---|
| 454 | /*
|
|---|
| 455 | Timer t(tv);
|
|---|
| 456 | cout << GetNodeName()<< " " <<t.GetTimeStr() << endl;
|
|---|
| 457 | */
|
|---|
| 458 | fPdoPos = (data[4]<<24) | (data[5]<<16) | (data[6]<<8) | data[7];
|
|---|
| 459 |
|
|---|
| 460 | // data[3]&0x01; // motor not moving
|
|---|
| 461 | fPosActive = data[3]&kPosActive; // positioning active
|
|---|
| 462 | fRpmActive = data[3]&kRpmActive; // RPM mode switched on
|
|---|
| 463 | // data[3]&0x08; // - unused -
|
|---|
| 464 | // data[3]&0x10; // - unused -
|
|---|
| 465 | // data[3]&0x20; // - unused -
|
|---|
| 466 | fInControl = data[3]&0x40; // motor uncontrolled
|
|---|
| 467 | // data[3]&0x80; // axis resetted (after errclr, motor stop, motor on)
|
|---|
| 468 |
|
|---|
| 469 | fStatus = data[3];
|
|---|
| 470 |
|
|---|
| 471 | fPdoTime.SetTimer(tv);
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | void Macs::HandlePDO2(BYTE_t *data, timeval_t *tv)
|
|---|
| 475 | {
|
|---|
| 476 | LWORDS_t errnum = (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | data[3];
|
|---|
| 477 | LWORDS_t errinf = (data[4]<<24) | (data[5]<<16) | (data[6]<<8) | data[7];
|
|---|
| 478 |
|
|---|
| 479 | //
|
|---|
| 480 | // errnum==0 gives a sudden information that something happened. Now the
|
|---|
| 481 | // microcontroller is running inside its interrupt procedure which
|
|---|
| 482 | // stopped the normal program. The interrupt procedure should try to clear
|
|---|
| 483 | // the error state of the hardware. This should never create a new error!
|
|---|
| 484 | //
|
|---|
| 485 | if (!errnum)
|
|---|
| 486 | {
|
|---|
| 487 | lout << "- " << GetNodeName() << ": reports Error occursion." << endl;
|
|---|
| 488 | lout << "Macs::HandlePDO2: " << GetNodeName() << " --> ZOMBIE!" << endl;
|
|---|
| 489 | SetZombie();
|
|---|
| 490 | SetError(-1);
|
|---|
| 491 | return;
|
|---|
| 492 | }
|
|---|
| 493 |
|
|---|
| 494 | //
|
|---|
| 495 | // Now the error is handled by the hardware now it is the software part
|
|---|
| 496 | // to react on it. The Error flag now is set to the correct value.
|
|---|
| 497 | //
|
|---|
| 498 | if (GetError()>0)
|
|---|
| 499 | {
|
|---|
| 500 | lout << GetNodeName() << ": WARNING! Error #" << GetError() << " unhandled (not cleared) by software." << endl;
|
|---|
| 501 |
|
|---|
| 502 | //
|
|---|
| 503 | // If the error is unhadled and/or not cleared, don't try it again.
|
|---|
| 504 | //
|
|---|
| 505 | if (GetError()==errnum)
|
|---|
| 506 | return;
|
|---|
| 507 | }
|
|---|
| 508 |
|
|---|
| 509 | SetError(errnum);
|
|---|
| 510 |
|
|---|
| 511 | lout << GetNodeName() << " reports: ";
|
|---|
| 512 | switch (errnum)
|
|---|
| 513 | {
|
|---|
| 514 | case 3:
|
|---|
| 515 | lout << "Axis does not existing." << endl;
|
|---|
| 516 | return;
|
|---|
| 517 | case 5:
|
|---|
| 518 | lout << "Error not cleared (while trying to move axis)" << endl;
|
|---|
| 519 | return;
|
|---|
| 520 | case 6:
|
|---|
| 521 | //
|
|---|
| 522 | // Report the error to the user. All possible movements should have
|
|---|
| 523 | // been stopped anyhow. Now delete the error to prevent the system
|
|---|
| 524 | // from reporting this error a thousands of times.
|
|---|
| 525 | //
|
|---|
| 526 | lout << "Home position not the first positioning command." << endl;
|
|---|
| 527 | SetError(0);
|
|---|
| 528 | return;
|
|---|
| 529 | case 8:
|
|---|
| 530 | lout << "Control deviation overflow." << endl;
|
|---|
| 531 | return;
|
|---|
| 532 | case 9:
|
|---|
| 533 | lout << "Zero index not found." << endl;
|
|---|
| 534 | return;
|
|---|
| 535 | case 10:
|
|---|
| 536 | lout << "Unknown command, syntax error." << endl;
|
|---|
| 537 | lout << "Please recompile and reload program." << endl;
|
|---|
| 538 | return;
|
|---|
| 539 | case 11:
|
|---|
| 540 | case 25:
|
|---|
| 541 | switch (errinf)
|
|---|
| 542 | {
|
|---|
| 543 | case -1:
|
|---|
| 544 | lout << "Negative";
|
|---|
| 545 | break;
|
|---|
| 546 | case 1:
|
|---|
| 547 | lout << "Positive";
|
|---|
| 548 | break;
|
|---|
| 549 | default:
|
|---|
| 550 | lout << "-unknown-";
|
|---|
| 551 | }
|
|---|
| 552 | switch (errnum)
|
|---|
| 553 | {
|
|---|
| 554 | case 11:
|
|---|
| 555 | lout << " software";
|
|---|
| 556 | break;
|
|---|
| 557 | case 25:
|
|---|
| 558 | lout << " hardware";
|
|---|
| 559 | break;
|
|---|
| 560 | }
|
|---|
| 561 | lout << " endswitch activated." << endl;
|
|---|
| 562 | return;
|
|---|
| 563 | case 12:
|
|---|
| 564 | lout << "Wrong parameter number used in SET command." << endl;
|
|---|
| 565 | return;
|
|---|
| 566 | case 14:
|
|---|
| 567 | lout << " Too many LOOP calls." << endl;
|
|---|
| 568 | return;
|
|---|
| 569 | case 16:
|
|---|
| 570 | lout << "Parameter in EEPROM broken (means: EEPROM broken, or saving not finished)" << endl;
|
|---|
| 571 | lout << "Please use APOSS to 'Reset' the MACS and reload the parameters." << endl;
|
|---|
| 572 | return;
|
|---|
| 573 | case 17:
|
|---|
| 574 | lout << "Program in EEPROM broken (means: EEPROM broken, or saving not finished)" << endl;
|
|---|
| 575 | lout << "Please use APOSS to delete all Programs restore the programs." << endl;
|
|---|
| 576 | return;
|
|---|
| 577 | case 18:
|
|---|
| 578 | lout << "Reset by CPU (reset called by Watch-dog cause of CPU halted)" << endl;
|
|---|
| 579 | lout << "Possible reasons: short under-/overvoltage or shortcut." << endl;
|
|---|
| 580 | return;
|
|---|
| 581 | case 19:
|
|---|
| 582 | lout << "User break (autostart program stopped by user)" << endl;
|
|---|
| 583 | return;
|
|---|
| 584 | case 51:
|
|---|
| 585 | lout << "Too many (>=10) GOSUB calls." << endl;
|
|---|
| 586 | return;
|
|---|
| 587 | case 52:
|
|---|
| 588 | lout << "Too many RETURN calls." << endl;
|
|---|
| 589 | return;
|
|---|
| 590 | case 62:
|
|---|
| 591 | lout << "Error veryfiing EEPROM after access (Try again savaing parameters or program)" << endl;
|
|---|
| 592 | return;
|
|---|
| 593 | case 70:
|
|---|
| 594 | lout << "Error in DIM call (call to DIM doesn't fit existing DIM call)" << endl;
|
|---|
| 595 | return;
|
|---|
| 596 | case 71:
|
|---|
| 597 | lout << "Array out of bound." << endl;
|
|---|
| 598 | return;
|
|---|
| 599 | case 79:
|
|---|
| 600 | lout << "Timeout waiting for index (WAITNDX)." << endl;
|
|---|
| 601 | return;
|
|---|
| 602 | case 84:
|
|---|
| 603 | lout << "Too many (>12) ON TIME calls." << endl;
|
|---|
| 604 | return;
|
|---|
| 605 | case 87:
|
|---|
| 606 | lout << "Out of memory for variables - Check APOSS predifined number of" << endl;
|
|---|
| 607 | lout << "variables and try deleting the array by doing a 'Reset' from APOSS." << endl;
|
|---|
| 608 | return;
|
|---|
| 609 | case 89:
|
|---|
| 610 | lout << "CAN I/O error (REOPEN=" << dec << errinf << " " << (errinf==0?"OK":"ERR") << ")" << endl;
|
|---|
| 611 | return;
|
|---|
| 612 |
|
|---|
| 613 | case 100:
|
|---|
| 614 | //lout << "Connection timed out." << endl;
|
|---|
| 615 | //EnableTimeout(false);
|
|---|
| 616 | return;
|
|---|
| 617 |
|
|---|
| 618 | default:
|
|---|
| 619 | lout << "Error Nr. " << errnum << ", " << errinf << endl;
|
|---|
| 620 | }
|
|---|
| 621 | }
|
|---|
| 622 |
|
|---|
| 623 | void Macs::HandlePDO3(BYTE_t *data, timeval_t *tv)
|
|---|
| 624 | {
|
|---|
| 625 | // 3 5 7 9
|
|---|
| 626 | // 1100 1010 1110 1001
|
|---|
| 627 | if (fStatusPdo3 == data[3])
|
|---|
| 628 | return;
|
|---|
| 629 |
|
|---|
| 630 | lout << GetNodeName() << ": Status PDO3 = ";
|
|---|
| 631 | const Bool_t ready = data[3]&0x01;
|
|---|
| 632 | const Bool_t fuse = data[3]&0x02;
|
|---|
| 633 | const Bool_t emcy = data[3]&0x04;
|
|---|
| 634 | const Bool_t vltg = data[3]&0x08;
|
|---|
| 635 | const Bool_t mode = data[3]&0x10;
|
|---|
| 636 | const Bool_t rf = data[3]&0x20;
|
|---|
| 637 | const Bool_t brake = data[3]&0x40;
|
|---|
| 638 | if (ready) lout << "DKC-Ready ";
|
|---|
| 639 | if (fuse) lout << "FuseOk ";
|
|---|
| 640 | if (emcy) lout << "EmcyOk ";
|
|---|
| 641 | if (vltg) lout << "OvervoltOk ";
|
|---|
| 642 | if (mode) lout << "SwitchToManualMode ";
|
|---|
| 643 | if (rf) lout << "RF ";
|
|---|
| 644 | if (brake) lout << "BrakeOpen ";
|
|---|
| 645 | lout << endl;
|
|---|
| 646 |
|
|---|
| 647 | fStatusPdo3 = data[3];
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | // FIXME? Handling of fIsZombie?
|
|---|
| 651 | void Macs::HandleError()
|
|---|
| 652 | {
|
|---|
| 653 | //
|
|---|
| 654 | // If there is no error we must not handle anything
|
|---|
| 655 | //
|
|---|
| 656 | if (!HasError())
|
|---|
| 657 | return;
|
|---|
| 658 |
|
|---|
| 659 | //
|
|---|
| 660 | // If the program got into the: HandleError state before the hardware
|
|---|
| 661 | // has finished handeling the error we have to wait for the hardware
|
|---|
| 662 | // handeling the error
|
|---|
| 663 | //
|
|---|
| 664 | // FIXME: Timeout???
|
|---|
| 665 | //
|
|---|
| 666 | // while (GetError()<0)
|
|---|
| 667 | // usleep(1);
|
|---|
| 668 |
|
|---|
| 669 | //
|
|---|
| 670 | // After this software and hardware should be in a state so that
|
|---|
| 671 | // we can go on working 'as usual' Eg. Initialize a Display Update
|
|---|
| 672 | //
|
|---|
| 673 | cout << GetNodeName() << " Handling Error #" << dec << GetError() << endl;
|
|---|
| 674 | switch (GetError())
|
|---|
| 675 | {
|
|---|
| 676 | case 6: // home
|
|---|
| 677 | case 8: // control dev
|
|---|
| 678 | case 9: // zero idx
|
|---|
| 679 | case 84: // ON TIME
|
|---|
| 680 | lout << "- " << GetNodeName() << ": Cannot handle error #" << GetError() << endl;
|
|---|
| 681 | return;
|
|---|
| 682 |
|
|---|
| 683 | case 11: // software endswitch
|
|---|
| 684 | case 25: // hardware endswitch
|
|---|
| 685 | lout << "- " << GetNodeName() << ": Cannot handle error 'Endswitch!'" << endl;
|
|---|
| 686 | return;
|
|---|
| 687 |
|
|---|
| 688 | case 100: // timeout (movement has been stopped, so we can go on)
|
|---|
| 689 | DelError();
|
|---|
| 690 | return;
|
|---|
| 691 | /*
|
|---|
| 692 | case 101:
|
|---|
| 693 | //lout << "Warning: " << GetNodeName() << " didn't respond in timeout window - try again." << endl;
|
|---|
| 694 | DelError();
|
|---|
| 695 | return;
|
|---|
| 696 | */
|
|---|
| 697 | default:
|
|---|
| 698 | lout << "- " << GetNodeName() << ": Cannot handle error #" << GetError() << endl;
|
|---|
| 699 |
|
|---|
| 700 | }
|
|---|
| 701 | }
|
|---|
| 702 |
|
|---|
| 703 | double Macs::GetTime()
|
|---|
| 704 | {
|
|---|
| 705 | return fPosTime.Now();
|
|---|
| 706 | }
|
|---|
| 707 |
|
|---|
| 708 | double Macs::GetMjd()
|
|---|
| 709 | {
|
|---|
| 710 | return fPosTime.GetMjd();
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | double Macs::GetPdoTime()
|
|---|
| 714 | {
|
|---|
| 715 | return fPdoTime.Now();
|
|---|
| 716 | }
|
|---|
| 717 |
|
|---|
| 718 | double Macs::GetPdoMjd()
|
|---|
| 719 | {
|
|---|
| 720 | return fPdoTime.GetMjd();
|
|---|
| 721 | }
|
|---|
| 722 |
|
|---|
| 723 | /* 0x2000 0 rw Maximum positioning error */
|
|---|
| 724 | /* 1 rw Negative Software Endswitch */
|
|---|
| 725 | /* 2 rw Positive Software Endswitch */
|
|---|
| 726 | void Macs::SetNegEndswitch(LWORDS_t val)
|
|---|
| 727 | {
|
|---|
| 728 | SendSDO(0x2000, 1, (LWORD_t)val);
|
|---|
| 729 | WaitForSdo(0x2000, 1);
|
|---|
| 730 | }
|
|---|
| 731 |
|
|---|
| 732 | void Macs::SetPosEndswitch(LWORDS_t val)
|
|---|
| 733 | {
|
|---|
| 734 | SendSDO(0x2000, 2, (LWORD_t)val);
|
|---|
| 735 | WaitForSdo(0x2000, 2);
|
|---|
| 736 | }
|
|---|
| 737 |
|
|---|
| 738 | void Macs::EnableEndswitches(bool neg, bool pos)
|
|---|
| 739 | {
|
|---|
| 740 | SendSDO(0x2000, 3, (LWORD_t)(neg|(pos<<1)));
|
|---|
| 741 | WaitForSdo(0x2000, 3);
|
|---|
| 742 | }
|
|---|
| 743 |
|
|---|
| 744 | void Macs::SendNodeguard()
|
|---|
| 745 | {
|
|---|
| 746 | SendSDO(0x4000, 0, (LWORD_t)0, false);
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | // --------------------------------------------------------------------------
|
|---|
| 750 | //
|
|---|
| 751 | // This starts the host guarding. The host guarding is only available
|
|---|
| 752 | // if the node guarding is running. The host guarding works with the
|
|---|
| 753 | // guardtime and the lifetimefactor from the nodeguarding.
|
|---|
| 754 | //
|
|---|
| 755 | void Macs::StartHostGuarding()
|
|---|
| 756 | {
|
|---|
| 757 | SendSDO(0x100c, 0, (LWORD_t)GetGuardTime());
|
|---|
| 758 | WaitForSdo(0x100c);
|
|---|
| 759 |
|
|---|
| 760 | SendSDO(0x100d, 0, (LWORD_t)GetLifeTimeFactor());
|
|---|
| 761 | WaitForSdo(0x100d);
|
|---|
| 762 |
|
|---|
| 763 | lout << "- " << GetNodeName() << ": Hostguarding started (" << dec;
|
|---|
| 764 | lout << GetLifeTimeFactor() << "*" << GetGuardTime() << "ms)" << endl;
|
|---|
| 765 | }
|
|---|
| 766 |
|
|---|
| 767 | // --------------------------------------------------------------------------
|
|---|
| 768 | //
|
|---|
| 769 | // Stop the host guarding.
|
|---|
| 770 | //
|
|---|
| 771 | void Macs::StopHostGuarding()
|
|---|
| 772 | {
|
|---|
| 773 | SendSDO(0x100c, 0, (LWORD_t)0);
|
|---|
| 774 | WaitForSdo(0x100c);
|
|---|
| 775 |
|
|---|
| 776 | SendSDO(0x100d, 0, (LWORD_t)0);
|
|---|
| 777 | WaitForSdo(0x100d);
|
|---|
| 778 |
|
|---|
| 779 | lout << "- " << GetNodeName() << ": Hostguarding stopped." << endl;
|
|---|
| 780 | }
|
|---|
| 781 |
|
|---|