static void HubDiscovered(const string& serial, const string& url) { // The call-back can be called several times for the same hub // (the discovery technique is based on a periodic broadcast) // So we use a dictionnary to avoid duplicates if (KnownHubs.find(serial) != KnownHubs.end()) return; cout << "hub found: " << serial << " (" << url << ")" << endl; // connect to the hub string msg; YAPI::RegisterHub(url, msg); // find the hub module YModule *hub = YModule::FindModule(serial); // iterate on all functions on the module and find the ports int fctCount = hub->functionCount(); for (int i = 0; i < fctCount; i++) { // retreive the hardware name of the ith function string fctHwdName = hub->functionId(i); if (fctHwdName.length() > 7 && fctHwdName.substr(0,7) == "hubPort") { // The port logical name is always the serial# // of the connected device string deviceid = hub->functionName(i); cout << " " << fctHwdName << " : " << deviceid << endl; } } // add the hub to the dictionnary so we won't have to // process is again. KnownHubs.insert(serial); // disconnect from the hub YAPI::UnregisterHub(url); }
/** * Switch the relay to the opposite state. * * @return YAPI_SUCCESS if the call succeeds. * * On failure, throws an exception or returns a negative error code. */ int YWatchdog::toggle(void) { int sta = 0; string fw; YModule* mo = NULL; if (_firm == 0) { mo = this->get_module(); fw = mo->get_firmwareRelease(); if (fw == YModule::FIRMWARERELEASE_INVALID) { return Y_STATE_INVALID; } _firm = atoi((fw).c_str()); } if (_firm < 34921) { sta = this->get_state(); if (sta == Y_STATE_INVALID) { return Y_STATE_INVALID; } if (sta == Y_STATE_B) { this->set_state(Y_STATE_A); } else { this->set_state(Y_STATE_B); } return YAPI_SUCCESS; } else { return this->_setAttr("state","X"); } }
int main(int argc, const char * argv[]) { string errmsg; // Setup the API to use local USB devices if(yRegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } if(argc < 2) usage(argv[0]); YModule *module = yFindModule(argv[1]); // use serial or logical name if (module->isOnline()) { if (argc >= 3) { string newname = argv[2]; if (!yCheckLogicalName(newname)) { cerr << "Invalid name (" << newname << ")" << endl; usage(argv[0]); } module->set_logicalName(newname); module->saveToFlash(); } cout << "Current name: " << module->get_logicalName() << endl; } else { cout << argv[1] << " not connected (check identification and USB cable)" << endl; } yFreeAPI(); return 0; }
int main(int argc, const char * argv[]) { string errmsg; string target; YVoltage *sensor; YVoltage *sensorAC; YVoltage *sensorDC; YModule *m; if (argc < 2) { usage(); } target = (string) argv[1]; YAPI::DisableExceptions(); // Setup the API to use local USB devices if (YAPI::RegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } if (target == "any") { // retreive any voltage sensor (can be AC or DC) sensor = YVoltage::FirstVoltage(); if (sensor == NULL) { cerr << "No module connected (Check cable)" << endl; exit(1); } } else { sensor = YVoltage::FindVoltage(target + ".voltage1"); } // we need to retreive both DC and AC voltage from the device. if (sensor->isOnline()) { m = sensor->get_module(); sensorDC = YVoltage::FindVoltage(m->get_serialNumber() + ".voltage1"); sensorAC = YVoltage::FindVoltage(m->get_serialNumber() + ".voltage2"); } else { cerr << "No module connected (Check cable)" << endl; exit(1); } while(1) { if (!sensorDC->isOnline()) { cout << "Module disconnected" << endl; break; } cout << "Voltage, DC : " << sensorDC->get_currentValue() << " v"; cout << " AC : " << sensorAC->get_currentValue() << " v"; cout << " (press Ctrl-C to exit)" << endl; YAPI::Sleep(1000, errmsg); }; yFreeAPI(); return 0; }
int main(int argc, const char * argv[]) { string errmsg; string target; YCurrent *sensor; YCurrent *sensorAC; YCurrent *sensorDC; YModule *m; if (argc < 2) usage(argv[0]); target = (string) argv[1]; // Setup the API to use local USB devices if (yRegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } if (target == "any") { // retreive any voltage sensor (can be AC or DC) sensor = yFirstCurrent(); if (sensor==NULL) die ("No module connected"); } else { sensor = yFindCurrent(target + ".current1"); } // we need to retreive both DC and AC voltage from the device. if (sensor->isOnline()) { m = sensor->get_module(); sensorDC = yFindCurrent(m->get_serialNumber() + ".current1"); sensorAC = yFindCurrent(m->get_serialNumber() + ".current2"); } else { die("Module not connected"); } while(1) { if (!m->isOnline()) die("Module not connected"); cout << "Current, DC : " << sensorDC->get_currentValue() << " mA"; cout << " AC : " << sensorAC->get_currentValue() << " mA"; cout << " (press Ctrl-C to exit)" << endl; ySleep(1000,errmsg); }; return 0; }
int main(int argc, const char * argv[]) { int i; string errmsg; vector<string> hubs; vector<string> shield; vector<string> devices; // Setup the API to use local USB devices if(YAPI::RegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } for (i = 1; i < argc; i++) { string hub_url = string(argv[i]); cout << "Update module connected to hub " << hub_url << endl; if(YAPI::RegisterHub(hub_url, errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } } //first step construct the list of all hub /shield and devices connected YModule *module = YModule::FirstModule(); while (module){ string product = module->get_productName(); string serial = module->get_serialNumber(); if (product == "YoctoHub-Shield") { shield.push_back(serial); } else if (product.substr(0,9) == "YoctoHub-") { hubs.push_back(serial); } else if (product != "VirtualHub"){ devices.push_back(serial); } module = module->nextModule(); } // first upgrades all Hubs... upgradeSerialList(hubs); // ... then all shield.. upgradeSerialList(shield); // ... and finaly all devices upgradeSerialList(devices); cout << "All devices are now up to date" << endl; YAPI::FreeAPI(); return 0; }
int main(int argc, const char * argv[]) { string errmsg; // Setup the API to use local USB devices if(yRegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } cout << "Device list: " << endl; YModule *module = yFirstModule(); while (module != NULL) { cout << module->get_serialNumber() << " "; cout << module->get_productName() << endl; module = module->nextModule(); } return 0; }
static int upgradeSerialList(vector<string> allserials) { string errmsg; for (std::vector<string>::iterator it = allserials.begin() ; it != allserials.end(); ++it) { string serial = *it; YModule *module = YModule::FindModule(serial); string product = module->get_productName(); string current = module->get_firmwareRelease(); // check if a new firmare is available on yoctopuce.com string newfirm = module->checkFirmware("www.yoctopuce.com", true); if (newfirm == "") { cout << product << " " << serial << "(rev=" << current << ") is up to date" << endl; } else { cout << product << " " << serial << "(rev=" << current << ") need be updated with firmare : " << endl; cout << " " << newfirm << endl; // execute the firmware upgrade YFirmwareUpdate update = module->updateFirmware(newfirm); int status = update.startUpdate(); do { int newstatus = update.get_progress(); if (newstatus != status) cout << newstatus << "% " << update.get_progressMessage() << endl; YAPI::Sleep(500, errmsg); status = newstatus; } while (status < 100 && status >= 0); if (status < 0) { cout << "Firmware Update failed: " << update.get_progressMessage() << endl; exit(1); } else { if (module->isOnline()) { cout << status << "% Firmware Updated Successfully!" << endl; } else { cout << status << " Firmware Update failed: module " << serial << " is not online" << endl; exit(1); } } } } return 0; }
int main(int argc, const char * argv[]) { string errmsg; // Setup the API to use local USB devices if(yRegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } if(argc < 2) usage(argv[0]); YModule *module = yFindModule(argv[1]); // use serial or logical name if (module->isOnline()) { if (argc > 2) { if (string(argv[2]) == "ON") module->set_beacon(Y_BEACON_ON); else module->set_beacon(Y_BEACON_OFF); } cout << "serial: " << module->get_serialNumber() << endl; cout << "logical name: " << module->get_logicalName() << endl; cout << "luminosity: " << module->get_luminosity() << endl; cout << "beacon: "; if (module->get_beacon()==Y_BEACON_ON) cout << "ON" << endl; else cout << "OFF" << endl; cout << "upTime: " << module->get_upTime()/1000 << " sec" << endl; cout << "USB current: " << module->get_usbCurrent() << " mA" << endl; cout << "Logs:"<< endl << module->get_lastLogs() << endl; } else { cout << argv[1] << " not connected (check identification and USB cable)" << endl; } return 0; }
int main(int argc, char **argv) { /** * The ros::init() function needs to see argc and argv so that it can perform * any ROS arguments and name remapping that were provided at the command line. For programmatic * remappings you can use a different version of init() which takes remappings * directly, but for most command-line programs, passing argc and argv is the easiest * way to do it. The third argument to init() is the name of the node. * * You must call one of the versions of ros::init() before using any other * part of the ROS system. */ ros::init(argc, argv, "yocto_PWM"); /** * NodeHandle is the main access point to communications with the ROS system. * The first NodeHandle constructed will fully initialize this node, and the last * NodeHandle destructed will close down the node. */ ros::NodeHandle n; /** * The advertise() function is how you tell ROS that you want to * publish on a given topic name. This invokes a call to the ROS * master node, which keeps a registry of who is publishing and who * is subscribing. After this advertise() call is made, the master * node will notify anyone who is trying to subscribe to this topic name, * and they will in turn negotiate a peer-to-peer connection with this * node. advertise() returns a Publisher object which allows you to * publish messages on that topic through a call to publish(). Once * all copies of the returned Publisher object are destroyed, the topic * will be automatically unadvertised. * * The second parameter to advertise() is the size of the message queue * used for publishing messages. If messages are published more quickly * than we can send them, the number here specifies how many messages to * buffer up before throwing some away. */ ros::Publisher pub = n.advertise<yocto::PWM_info>("/yocto/pwm_info", 1000); //avisa que irá publicar no tópico /yocto/pwm_info ros::Rate loop_rate(10); string errmsg; string target1 = "YPWMRX01-37171"; string target2 = "YPWMRX01-43C43"; YPwmInput *pwm; YPwmInput *pwm1; YPwmInput *pwm2; YPwmInput *pwm3; YPwmInput *pwm4; YModule *m; YAPI::DisableExceptions(); // ROS_INFO("Node yocto rodando!\n"); // Setup the API to use local USB devices if (YAPI::RegisterHub("usb", errmsg) != YAPI_SUCCESS) { cerr << "RegisterHub error: " << errmsg << endl; return 1; } // retreive any pwm input available pwm = YPwmInput::FindPwmInput(target1 + ".pwmInput1"); if (pwm == NULL) { cerr << "No module connected (Check cable)" << endl; exit(1); } // we need to retreive both channels from the device. if (pwm->isOnline()) { m = pwm->get_module(); pwm1 = YPwmInput::FindPwmInput(m->get_serialNumber() + ".pwmInput1"); pwm2 = YPwmInput::FindPwmInput(m->get_serialNumber() + ".pwmInput2"); } else { cerr << "No module connected (Check cable)" << endl; exit(1); } pwm = YPwmInput::FindPwmInput(target2 + ".pwmInput1"); if (pwm == NULL) { cerr << "No module connected (Check cable)" << endl; exit(1); } // we need to retreive both channels from the device. if (pwm->isOnline()) { m = pwm->get_module(); pwm3 = YPwmInput::FindPwmInput(m->get_serialNumber() + ".pwmInput1"); pwm4 = YPwmInput::FindPwmInput(m->get_serialNumber() + ".pwmInput2"); } else { cerr << "No module connected (Check cable)" << endl; exit(1); } yocto::PWM_info mtr; //objeto da mensagem que será publicada while (ros::ok()) { mtr.frequency_1 = pwm1->get_frequency(); mtr.duty_cycle_1 = pwm1->get_dutyCycle(); mtr.frequency_2 = pwm2->get_frequency(); mtr.duty_cycle_2 = pwm2->get_dutyCycle(); mtr.frequency_3 = pwm3->get_frequency(); mtr.duty_cycle_3 = pwm3->get_dutyCycle(); mtr.frequency_4 = pwm4->get_frequency(); mtr.duty_cycle_4 = pwm4->get_dutyCycle(); ROS_INFO("-----------------------------\n"); ROS_INFO("Frequency: %f", mtr.frequency_1); ROS_INFO("Duty Cycle 1: %f", mtr.duty_cycle_1); // ROS_INFO("Frequency: %f", mtr.frequency_2); ROS_INFO("Duty Cycle 2: %f", mtr.duty_cycle_2); // ROS_INFO("Frequency: %f", mtr.frequency_3); ROS_INFO("Duty Cycle 3: %f", mtr.duty_cycle_3); // ROS_INFO("Frequency: %f", mtr.frequency_4); ROS_INFO("Duty Cycle 4: %f", mtr.duty_cycle_4); /** * The publish() function is how you send messages. The parameter * is the message object. The type of this object must agree with the type * given as a template parameter to the advertise<>() call, as was done * in the constructor above. */ pub.publish(mtr); ros::spinOnce(); loop_rate.sleep(); } return 0; }