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); }
// link the instance to a real YoctoAPI object internal override void linkToHardware(string hwdName) { YMotor hwd = YMotor.FindMotor(hwdName); // first redo base_init to update all _func pointers base_init(hwd, hwdName); // then setup Yocto-API pointers and callbacks init(hwd); }
// perform the 2nd stage setup that requires YoctoAPI object protected void init(YMotor hwd) { if (hwd == null) { return; } base.init(hwd); InternalStuff.log("registering Motor callback"); _func.registerValueCallback(valueChangeCallback); }
/** * <summary> * Retrieves a motor for a given identifier. * <para> * The identifier can be specified using several formats: * </para> * <para> * </para> * <para> * - FunctionLogicalName * </para> * <para> * - ModuleSerialNumber.FunctionIdentifier * </para> * <para> * - ModuleSerialNumber.FunctionLogicalName * </para> * <para> * - ModuleLogicalName.FunctionIdentifier * </para> * <para> * - ModuleLogicalName.FunctionLogicalName * </para> * <para> * </para> * <para> * This function does not require that the motor is online at the time * it is invoked. The returned object is nevertheless valid. * Use the method <c>YMotor.isOnline()</c> to test if the motor is * indeed online at a given time. In case of ambiguity when looking for * a motor by logical name, no error is notified: the first instance * found is returned. The search is performed first by hardware name, * then by logical name. * </para> * </summary> * <param name="func"> * a string that uniquely characterizes the motor * </param> * <returns> * a <c>YMotor</c> object allowing you to drive the motor. * </returns> */ public static YMotor FindMotor(string func) { YMotor obj; obj = (YMotor)YFunction._FindFromCache("Motor", func); if (obj == null) { obj = new YMotor(func); YFunction._AddToCache("Motor", func, obj); } return(obj); }
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(); }
/** * <summary> * Retrieves a motor for a given identifier. * <para> * The identifier can be specified using several formats: * </para> * <para> * </para> * <para> * - FunctionLogicalName * </para> * <para> * - ModuleSerialNumber.FunctionIdentifier * </para> * <para> * - ModuleSerialNumber.FunctionLogicalName * </para> * <para> * - ModuleLogicalName.FunctionIdentifier * </para> * <para> * - ModuleLogicalName.FunctionLogicalName * </para> * <para> * </para> * <para> * This function does not require that the motor is online at the time * it is invoked. The returned object is nevertheless valid. * Use the method <c>YMotor.isOnline()</c> to test if the motor is * indeed online at a given time. In case of ambiguity when looking for * a motor by logical name, no error is notified: the first instance * found is returned. The search is performed first by hardware name, * then by logical name. * </para> * </summary> * <param name="func"> * a string that uniquely characterizes the motor * </param> * <returns> * a <c>YMotor</c> object allowing you to drive the motor. * </returns> */ public static YMotor FindMotor(string func) { YMotor obj; obj = (YMotor) YFunction._FindFromCache("Motor", func); if (obj == null) { obj = new YMotor(func); YFunction._AddToCache("Motor", func, obj); } return obj; }
// perform the initial setup that may be done without a YoctoAPI object (hwd can be null) internal override void base_init(YFunction hwd, string instantiationName) { _func = (YMotor)hwd; base.base_init(hwd, instantiationName); }
//--- (end of YMotor definitions) //--- (YMotor implementation) internal YMotorProxy(YMotor hwd, string instantiationName) : base(hwd, instantiationName) { InternalStuff.log("Motor " + instantiationName + " instantiation"); base_init(hwd, instantiationName); }