remotecontrolboard.cpp
来自「一个语言识别引擎」· C++ 代码 · 共 2,110 行 · 第 1/5 页
CPP
2,110 行
bool ok = rpc_p.write(cmd, response);
if (CHECK_FAIL(ok, response)) {
// ok
*val = response.get(2).asDouble();
return true;
}
return false;
}
/**
* Helper method to get an array of double from the remote peer.
* @param v is the name of the command
* @param val is the array of double
* @return true/false on success/failure
*/
bool getDoubleArray(int v, double *val) {
Bottle cmd, response;
cmd.addVocab(VOCAB_GET);
cmd.addVocab(v);
bool ok = rpc_p.write(cmd, response);
if (CHECK_FAIL(ok, response)) {
int i;
Bottle& l = *(response.get(2).asList());
if (&l == 0)
return false;
int njs = l.size();
ACE_ASSERT (nj == njs);
for (i = 0; i < nj; i++)
val[i] = l.get(i).asDouble();
return true;
}
return false;
}
public:
/**
* Constructor.
*/
RemoteControlBoard() {
nj = 0;
}
/**
* Destructor.
*/
virtual ~RemoteControlBoard() {
}
/**
* Default open.
* @return always true.
*/
virtual bool open() {
return true;
}
virtual bool open(Searchable& config) {
remote = config.find("remote").asString().c_str();
local = config.find("local").asString().c_str();
if (local != "") {
String s1 = local;
s1 += "/rpc:o";
rpc_p.open(s1.c_str());
s1 = local;
s1 += "/command:o";
command_p.open(s1.c_str());
s1 = local;
s1 += "/state:i";
state_p.open(s1.c_str());
}
if (remote != "") {
String s1 = remote;
s1 += "/rpc:i";
String s2 = local;
s2 += "/rpc:o";
// connect twice for RPC ports.
Network::connect(s2.c_str(), s1.c_str());
Network::connect(s1.c_str(), s2.c_str());
s1 = remote;
s1 += "/command:i";
s2 = local;
s2 += "/command:o";
Network::connect(s2.c_str(), s1.c_str());
s1 = remote;
s1 += "/state:o";
s2 = local;
s2 += "/state:i";
Network::connect(s1.c_str(), s2.c_str(), "mcast");
}
state_buffer.attach(state_p);
state_buffer.setStrict(false);
command_buffer.attach(command_p);
bool ok = getCommand(VOCAB_AXES, nj);
fprintf(stderr, "AXES:%d", nj);
if (!ok) {
ACE_OS::printf("Problems with obtaining the number of controlled axes\n");
return false;
}
return true;
}
/**
* Close the device driver and stop the port connections.
* @return true/false on success/failure.
*/
virtual bool close() {
rpc_p.close();
command_p.close();
state_p.close();
return true;
}
/**
* Set new pid value for a joint axis.
* @param j joint number
* @param pid new pid value
* @return true/false on success/failure
*/
virtual bool setPid(int j, const Pid &pid) {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_PID);
cmd.addInt(j);
Bottle& l = cmd.addList();
l.addDouble(pid.kp);
l.addDouble(pid.kd);
l.addDouble(pid.ki);
l.addDouble(pid.max_int);
l.addDouble(pid.max_output);
l.addDouble(pid.offset);
l.addDouble(pid.scale);
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/**
* Set new pid value on multiple axes.
* @param pids pointer to a vector of pids
* @return true/false upon success/failure
*/
virtual bool setPids(const Pid *pids) {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_PIDS);
Bottle& l = cmd.addList();
int i;
for (i = 0; i < nj; i++) {
Bottle& m = l.addList();
m.addDouble(pids[i].kp);
m.addDouble(pids[i].kd);
m.addDouble(pids[i].ki);
m.addDouble(pids[i].max_int);
m.addDouble(pids[i].max_output);
m.addDouble(pids[i].offset);
m.addDouble(pids[i].scale);
}
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/**
* Set the controller reference point for a given axis.
* Warning this method can result in very large torques
* and should be used carefully. If you do not understand
* this warning you should avoid using this method.
* Have a look at other interfaces (e.g. position control).
* @param j joint number
* @param ref new reference point
* @return true/false upon success/failure
*/
virtual bool setReference(int j, double ref) {
return setDouble(VOCAB_REF, j, ref);
}
/**
* Set the controller reference points, multiple axes.
* Warning this method can result in very large torques
* and should be used carefully. If you do not understand
* this warning you should avoid using this method.
* Have a look at other interfaces (e.g. position control).
* @param refs pointer to the vector that contains the new reference points.
* @return true/false upon success/failure
*/
virtual bool setReferences(const double *refs) {
return setDoubleArray(VOCAB_REFS, refs);
}
/**
* Set the error limit for the controller on a specifi joint
* @param j joint number
* @param limit limit value
* @return true/false on success/failure
*/
virtual bool setErrorLimit(int j, double limit) {
return setDouble(VOCAB_LIM, j, limit);
}
/**
* Get the error limit for the controller on all joints.
* @param limits pointer to the vector with the new limits
* @return true/false on success/failure
*/
virtual bool setErrorLimits(const double *limits) {
return setDoubleArray(VOCAB_LIMS, limits);
}
/**
* Get the current error for a joint.
* @param j joint number
* @param err pointer to the storage for the return value
* @return true/false on success failure
*/
virtual bool getError(int j, double *err) {
return getDouble(VOCAB_ERR, j, err);
}
/** Get the error of all joints.
* @param errs pointer to the vector that will store the errors
*/
virtual bool getErrors(double *errs) {
return getDoubleArray(VOCAB_ERRS, errs);
}
/**
* Get the output of the controller (e.g. pwm value)
* @param j joint number
* @param out pointer to storage for return value
* @return success/failure
*/
virtual bool getOutput(int j, double *out) {
return getDouble(VOCAB_OUTPUT, j, out);
}
/**
* Get the output of the controllers (e.g. pwm value)
* @param outs pinter to the vector that will store the output values
* @return true/false on success/failure
*/
virtual bool getOutputs(double *outs) {
return getDoubleArray(VOCAB_OUTPUTS, outs);
}
/**
* Get current pid value for a specific joint.
* @param j joint number
* @param pid pointer to storage for the return value.
* @return success/failure
*/
virtual bool getPid(int j, Pid *pid) {
Bottle cmd, response;
cmd.addVocab(VOCAB_GET);
cmd.addVocab(VOCAB_PID);
cmd.addInt(j);
bool ok = rpc_p.write(cmd, response);
if (CHECK_FAIL(ok, response)) {
Bottle& l = *(response.get(2).asList());
if (&l == 0)
return false;
pid->kp = l.get(0).asDouble();
pid->kd = l.get(1).asDouble();
pid->ki = l.get(2).asDouble();
pid->max_int = l.get(3).asDouble();
pid->max_output = l.get(4).asDouble();
pid->offset = l.get(5).asDouble();
pid->scale = l.get(6).asDouble();
return true;
}
return false;
}
/**
* Get current pid value for all controlled axes.
* @param pids vector that will store the values of the pids.
* @return success/failure
*/
virtual bool getPids(Pid *pids) {
Bottle cmd, response;
cmd.addVocab(VOCAB_GET);
cmd.addVocab(VOCAB_PIDS);
bool ok = rpc_p.write(cmd, response);
if (CHECK_FAIL(ok, response)) {
int i;
Bottle& l = *(response.get(2).asList());
if (&l == 0)
return false;
const int njs = l.size();
ACE_ASSERT (njs == nj);
for (i = 0; i < nj; i++)
{
Bottle& m = *(l.get(i).asList());
if (&m == 0)
return false;
pids->kp = m.get(0).asDouble();
pids->kd = m.get(1).asDouble();
pids->ki = m.get(2).asDouble();
pids->max_int = m.get(3).asDouble();
pids->max_output = m.get(4).asDouble();
pids->offset = m.get(5).asDouble();
pids->scale = m.get(6).asDouble();
}
return true;
}
return false;
}
/**
* Get the current reference position of the controller for a specific joint.
* @param j is joint number
* @param ref pointer to storage for return value
* @return true/false on success/failure
*/
virtual bool getReference(int j, double *ref) {
return getDouble(VOCAB_REF, j, ref);
}
/** Get the current reference position of all controllers.
* @param refs vector that will store the output.
*/
virtual bool getReferences(double *refs) {
return getDoubleArray(VOCAB_REFS, refs);
}
/**
* Get the error limit for the controller on a specific joint
* @param j is the joint number
* @param limit pointer to storage
* @return true/false on success/failure
*/
virtual bool getErrorLimit(int j, double *limit) {
return getDouble(VOCAB_LIM, j, limit);
}
/**
* Get the error limit for all controllers
* @param limits pointer to the array that will store the output
* @return true/false on success/failure
*/
virtual bool getErrorLimits(double *limits) {
return getDoubleArray(VOCAB_LIMS, limits);
}
/**
* Reset the controller of a given joint, usually sets the
* current position of the joint as the reference value for the PID, and resets
* the integrator.
* @param j joint number
* @return true on success, false on failure.
*/
virtual bool resetPid(int j) {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_RESET);
cmd.addInt(j);
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/**
* Disable the pid computation for a joint
* @param j is the joint number
* @return true/false on success/failure
*/
virtual bool disablePid(int j) {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_DISABLE);
cmd.addInt(j);
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/**
* Enable the pid computation for a joint
* @param j is the joint number
* @return true/false on success/failure
*/
virtual bool enablePid(int j) {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_ENABLE);
cmd.addInt(j);
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/* IEncoder */
/**
* Reset encoder, single joint. Set the encoder value to zero
* @param j is the axis number
* @return true/false on success/failure
*/
virtual bool resetEncoder(int j) {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_E_RESET);
cmd.addInt(j);
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/**
* Reset encoders. Set the encoders value to zero
* @return true/false
*/
virtual bool resetEncoders() {
Bottle cmd, response;
cmd.addVocab(VOCAB_SET);
cmd.addVocab(VOCAB_E_RESETS);
bool ok = rpc_p.write(cmd, response);
return CHECK_FAIL(ok, response);
}
/**
* Set the value of the encoder for a given joint.
* @param j encoder number
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?