public override async Task <int> Run() { try { await YAPI.RegisterHub(HubURL); YMotor motor; YCurrent current; YVoltage voltage; YTemperature temperature; if (Target.ToLower() == "any") { // find the serial# of the first available motor motor = YMotor.FirstMotor(); if (motor == null) { WriteLine("No module connected (check USB cable) "); return(-1); } Target = await(await motor.get_module()).get_serialNumber(); } int power = Convert.ToInt32(Power); motor = YMotor.FindMotor(Target + ".motor"); current = YCurrent.FindCurrent(Target + ".current"); voltage = YVoltage.FindVoltage(Target + ".voltage"); temperature = YTemperature.FindTemperature(Target + ".temperature"); // lets start the motor if (await motor.isOnline()) { // if motor is in error state, reset it. if (await motor.get_motorStatus() >= YMotor.MOTORSTATUS_LOVOLT) { await motor.resetStatus(); } await motor.drivingForceMove(power, 2000); // ramp up to power in 2 seconds while (await motor.isOnline()) { // display motor status WriteLine("Status=" + await motor.get_advertisedValue() + " " + "Voltage=" + await voltage.get_currentValue() + "V " + "Current=" + await current.get_currentValue() / 1000 + "A " + "Temp=" + await temperature.get_currentValue() + "deg C"); await YAPI.Sleep(1000); // wait for one second } } } catch (YAPI_Exception ex) { WriteLine("error: " + ex.Message); } YAPI.FreeAPI(); return(0); }
public static YMotorProxy FindMotor(string name) { // cases to handle: // name ="" no matching unknwn // name ="" unknown exists // name != "" no matching unknown // name !="" unknown exists YMotor func = null; YMotorProxy res = (YMotorProxy)YFunctionProxy.FindSimilarUnknownFunction("YMotorProxy"); if (name == "") { if (res != null) { return(res); } res = (YMotorProxy)YFunctionProxy.FindSimilarKnownFunction("YMotorProxy"); if (res != null) { return(res); } func = YMotor.FirstMotor(); if (func != null) { name = func.get_hardwareId(); if (func.get_userData() != null) { return((YMotorProxy)func.get_userData()); } } } else { func = YMotor.FindMotor(name); if (func.get_userData() != null) { return((YMotorProxy)func.get_userData()); } } if (res == null) { res = new YMotorProxy(func, name); } if (func != null) { res.linkToHardware(name); if (func.isOnline()) { res.arrival(); } } return(res); }
/** * <summary> * Enumerates all functions of type Motor available on the devices * currently reachable by the library, and returns their unique hardware ID. * <para> * Each of these IDs can be provided as argument to the method * <c>YMotor.FindMotor</c> to obtain an object that can control the * corresponding device. * </para> * </summary> * <returns> * an array of strings, each string containing the unique hardwareId * of a device function currently connected. * </returns> */ public static new string[] GetSimilarFunctions() { List <string> res = new List <string>(); YMotor it = YMotor.FirstMotor(); while (it != null) { res.Add(it.get_hardwareId()); it = it.nextMotor(); } return(res.ToArray()); }
static void Main(string[] args) { string errmsg = ""; string target; int power; YMotor motor; YCurrent current; YVoltage voltage; YTemperature temperature; // parse the command line if (args.Length < 2) { usage(); } target = args[0].ToUpper(); power = Convert.ToInt32(args[1]); if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS) { Console.WriteLine("RegisterHub error: " + errmsg); Environment.Exit(0); } if (target == "ANY") { // find the serial# of the first available motor motor = YMotor.FirstMotor(); if (motor == null) { Console.WriteLine("No module connected (check USB cable) "); Environment.Exit(0); } target = motor.get_module().get_serialNumber(); } motor = YMotor.FindMotor(target + ".motor"); current = YCurrent.FindCurrent(target + ".current"); voltage = YVoltage.FindVoltage(target + ".voltage"); temperature = YTemperature.FindTemperature(target + ".temperature"); // lets start the motor if (motor.isOnline()) { // if motor is in error state, reset it. if (motor.get_motorStatus() >= YMotor.MOTORSTATUS_LOVOLT) { motor.resetStatus(); } motor.drivingForceMove(power, 2000); // ramp up to power in 2 seconds while (motor.isOnline()) { // display motor status Console.WriteLine("Status=" + motor.get_advertisedValue() + " " + "Voltage=" + voltage.get_currentValue() + "V " + "Current=" + current.get_currentValue() / 1000 + "A " + "Temp=" + temperature.get_currentValue() + "deg C"); YAPI.Sleep(1000, ref errmsg); // wait for one second } } YAPI.FreeAPI(); }