Пример #1
0
        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);
        }
        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);
        }
Пример #4
0
        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();
        }