60 #include "rnr/rnrconfig.h" 61 #include "rnr/units.h" 64 #include "rnr/serdev.h" 70 int SerDevReadline(
int fd, byte_t *buf,
int timeout)
75 while ((c = (
char)SerDevGetc(fd, timeout)) !=
'\r')
80 fprintf(stderr,
"response received: %s\n", sstr.str().c_str());
82 strcpy((
char*)buf,sstr.str().c_str());
84 return sstr.str().size();
89 LOGDIAG3(
"jnt devName= %s baudRate= %d \n", devName.c_str(), baudRate);
90 if ((m_fd=SerDevOpen(devName.c_str(),baudRate,8,
'N',1,0,0))==-1)
92 fprintf(stderr,
"dhp -here\n");
95 fprintf(stderr,
"dhp -here 2\n");
101 return SerDevClose(m_fd);
105 int MotRoboteqSmall::sendCommand(
int fd, byte_t* buf,
int nBytes,
int timeout)
107 SerDevWrite(fd, buf, nBytes, timeout);
108 fprintf(stderr,
"sent command: %s\n", buf);
113 int MotRoboteqSmall::recvResponse(
int fd, byte_t *buf,
int timeout)
122 SerDevReadline(fd, buf, timeout);
126 nBytes = SerDevReadline(fd, buf, timeout);
133 if (motID >2 || motID <1)
135 LOGDIAG3(
"Not a valid motor ID.\n");
144 if (!motIDIsValid(motID))
149 if (units == units_norm)
151 if (speed >1 || speed <-1)
153 LOGDIAG3(
"Not a valid normalized motor speed.\n");
156 rawSpeed=(int)(speed*1000);
159 return setSpeedRaw(motID, rawSpeed);
164 VecSpeedTupples::iterator iter;
165 VecSpeedRawTupples vecSpeedRawTupple;
166 for (iter=vecSpeedTupple.begin();iter!=vecSpeedTupple.end();iter++)
170 if (!motIDIsValid(src.m_nMotID))
174 dest.m_nMotID = src.m_nMotID;
175 if (src.m_fVal >1 || src.m_fVal <-1)
177 LOGDIAG3(
"Not a valid normalized motor speed. \n");
180 dest.m_nVal = (int)(1000 * src.m_fVal);
181 vecSpeedRawTupple.push_back(dest);
183 setSpeedRaw(vecSpeedRawTupple);
187 int MotRoboteqSmall::setSpeedRaw(
int motID,
int rawSpeed)
192 sprintf(cmd,
"!G %d %d\r", motID, rawSpeed);
193 int nBytes = strlen(cmd);
195 sendCommand(m_fd, (byte_t*)cmd, nBytes, m_ntimeout);
196 nBytes = recvResponse(m_fd, (byte_t*)res, m_ntimeout);
200 LOGERROR(
"setSpeedRaw(id,speed) command failed.\n");
205 int MotRoboteqSmall::setSpeedRaw(VecSpeedRawTupples vecSpeedRawTupple)
210 VecSpeedRawTupples::iterator iter;
211 for (iter=vecSpeedRawTupple.begin();iter!=vecSpeedRawTupple.end();iter++)
213 str <<
"!G " << (*iter).m_nMotID <<
" " << (*iter).m_nVal <<
"_";
216 sendCommand(m_fd, (byte_t *)str.str().c_str(), str.str().length(), m_ntimeout);
219 for(
int i=0; i < vecSpeedRawTupple.size(); i++)
221 int nBytes = recvResponse(m_fd, (byte_t*)buf , m_ntimeout);
226 LOGERROR(
"setSpeedRaw(vec) command failed\n");
232 int MotRoboteqSmall::stop(
int motID)
234 setSpeedRaw(motID, 0);
237 int MotRoboteqSmall::stop()
252 sprintf(cmd,
"!EX\r");
253 int nBytes = strlen(cmd);
255 sendCommand(m_fd, (byte_t*)cmd, nBytes, m_ntimeout);
256 recvResponse(m_fd, (byte_t*)res, m_ntimeout);
260 LOGERROR(
"setSpeedRaw(vec) command failed\nIs firmware updated?\n");
274 sprintf(cmd,
"!MG\r");
275 int nBytes = strlen(cmd);
277 sendCommand(m_fd, (byte_t*)cmd, nBytes, m_ntimeout);
278 recvResponse(m_fd, (byte_t*)res, m_ntimeout);
282 LOGERROR(
"setSpeedRaw(vec) command failed\nIs firmware updated?\n");
289 if (!motIDIsValid(motID))
296 sprintf(cmd,
"?A %d\r", motID);
297 int nBytes = strlen(cmd);
299 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
300 recvResponse(m_fd, (byte_t *)res, m_ntimeout);
311 sprintf(cmd,
"~ALIM\r");
312 int nBytes = strlen(cmd);
314 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
315 recvResponse(m_fd, (byte_t*)res, m_ntimeout);
329 int rawCurrent = current *10;
330 sprintf(cmd,
"^ALIM %d %d\r", motID, rawCurrent);
331 int nBytes = strlen(cmd);
333 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
334 recvResponse(m_fd, (byte_t*)res, m_ntimeout);
339 LOGERROR(
"setSpeedRaw(vec) command failed\n");
350 sprintf(cmd,
"~UVL_~OVL\r");
351 int nBytes = strlen(cmd);
353 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
355 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
357 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
367 int nBytes = strlen(cmd);
369 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
370 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
380 LOGDIAG3(
"Not a valid lower voltage limit. Must be greater than 5V \n");
384 int lowVoltageRaw=lowVoltage*10;
385 int overVoltageRaw=overVoltage*10;
391 sprintf(cmd,
"^UVL %d_^OVL %d\r", lowVoltageRaw, overVoltageRaw);
392 int nBytes = strlen(cmd);
394 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
395 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
399 LOGERROR(
"setSpeedRaw(vec) command failed\n");
402 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
406 LOGERROR(
"setSpeedRaw(vec) command failed\n");
425 sprintf(cmd,
"^MAC %d %d_^MDEC %d %d\r", motID, accel, motID, decel);
426 int nBytes = strlen(cmd);
428 sendCommand(m_fd, (byte_t *)cmd, nBytes, m_ntimeout);
429 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
433 LOGERROR(
"setSpeedProfile() command failed\n");
436 recvResponse(m_fd,(byte_t *)res, m_ntimeout);
440 LOGERROR(
"setSpeedProfile() command failed\n");
int setVoltageLimits(int lowVoltage, int overVoltage)
Function sets the minimum and maximum limits for applied voltage for all motors.
int eStop()
Function simultaneously halts motor rotation for all motors by setting speeds to zero.
int getCurrent(int motID, units_t units=units_amp)
Function retrieves value for current load on a single motor.
int eStopRelease()
Function releases emergency stop condition and allows normal ops.
int setSpeedProfile(int motID, int accel, int decel=DEF_DEC)
Function retrieves value for voltage applied to a single motor.
Common Human Interface Device Interface.
int setSpeed(int motID, float speed, units_t units=units_norm)
Description.
int setCurrentLimits(int motID, int current, units_t units=units_amp)
Function sets minimum and maximum limits for current.
int open(const std::string &devName, int baudRate)
Function opens serial communication with motor controller device.
bool motIDIsValid(int motID)
Function sends commands to the motor controller and receives responses from the motor controller...
int close()
Function closes serial communication with motor controller device.
int getVoltageLimits()
Function retrieves value for current load on a single motor.
RoadNarrows Robotics standard namespace.
int getCurrentLimits()
Function determines if a motor controller can monitor current.
int getVoltage()
Function retrieves value for voltage applied to a single motor.