Esempio n. 1
0
        private void testController()
        {
            Console.WriteLine("Testing constoller ...");
            EV3Brick ev3 = new EV3Brick();

            ev3.connect();

            // current status: normalize
            int motorAdeg = (int)(ev3.getMotorADegree() / 50.0);
            int motorBdeg = (int)(ev3.getMotorBDegree() / 300.0);

            /*
             * training data = [
             *      [[0,0,0,1], [0,1]],		// down1
             *      [[0,1,1,1], [1,0]],		// pick
             *      [[1,1,1,0], [0,-1]],	// up1
             *      [[1,0,1,1], [0,1]],		// down2
             *      [[1,1,0,1], [-1,0]],	// drop
             *      [[0,1,0,0], [0,-1]]		// up2
             * ]
             */
            var argv = new List <int[]> ();

            // current (A, B), desired (A, B)
            // down1
            argv.Add(new [] { motorAdeg, motorBdeg, 0, 1 });
//			argv.Add (new []{0,0,0,1});
            argv.Add(new [] { 0, 1 });                  // targets A, B

            // up1
//			argv.Add (new []{1,1,1,0});
//			argv.Add (new []{motorAdeg,motorBdeg,1,0});
//			argv.Add (new []{0,-1});		// targets A, B

            var engine = Python.CreateEngine();
            var paths  = engine.GetSearchPaths();

            paths.Add(pythonPath);
            engine.SetSearchPaths(paths);
            engine.GetSysModule().SetVariable("argv", argv);

            var scriptRuntime = Python.CreateRuntime();

            scriptRuntime.GetSysModule().SetVariable("argv", argv);

            try
            {
                dynamic result = engine.ExecuteFile("script.py");
                Console.WriteLine(result.outputs);
                var outputs = result.outputs;
                foreach (double output in outputs)
                {
                    Console.WriteLine(output);
                }
                motorAdeg = (int)(outputs[0] * 50);
                motorBdeg = (int)(outputs[1] * 300);
                Console.WriteLine(motorAdeg + ", " + motorBdeg);
                ev3.moveMotorA(motorAdeg);
                ev3.moveMotorB(motorBdeg);
                Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                Console.WriteLine(
                    "Oops! We couldn't execute the script because of an exception: " + ex.Message);
            }

            ev3.disconnect();

            Console.WriteLine("Complete.");
            //			Console.ReadLine();
        }
Esempio n. 2
0
File: ev3.cs Progetto: dpiquee/EV3
        private void testController()
        {
            Console.WriteLine("Testing constoller ...");
            EV3Brick ev3 = new EV3Brick ();
            ev3.connect ();

            // current status: normalize
            int motorAdeg = (int) (ev3.getMotorADegree()/50.0);
            int motorBdeg = (int) (ev3.getMotorBDegree()/300.0);

            /*
            training data = [
                [[0,0,0,1], [0,1]],		// down1
                [[0,1,1,1], [1,0]],		// pick
                [[1,1,1,0], [0,-1]],	// up1
                [[1,0,1,1], [0,1]],		// down2
                [[1,1,0,1], [-1,0]],	// drop
                [[0,1,0,0], [0,-1]]		// up2
            ]
            */
            var argv = new List<int[]> ();
            // current (A, B), desired (A, B)
            // down1
            argv.Add (new []{motorAdeg,motorBdeg,0,1});
            //			argv.Add (new []{0,0,0,1});
            argv.Add (new []{0,1});		// targets A, B

            // up1
            //			argv.Add (new []{1,1,1,0});
            //			argv.Add (new []{motorAdeg,motorBdeg,1,0});
            //			argv.Add (new []{0,-1});		// targets A, B

            var engine = Python.CreateEngine();
            var paths = engine.GetSearchPaths();
            paths.Add(pythonPath);
            engine.SetSearchPaths(paths);
            engine.GetSysModule ().SetVariable ("argv", argv);

            var scriptRuntime = Python.CreateRuntime();
            scriptRuntime.GetSysModule().SetVariable("argv", argv);

            try
            {
                dynamic result = engine.ExecuteFile("script.py");
                Console.WriteLine(result.outputs);
                var outputs = result.outputs;
                foreach (double output in outputs) {
                    Console.WriteLine(output);
                }
                motorAdeg = (int) (outputs[0]*50);
                motorBdeg = (int) (outputs[1]*300);
                Console.WriteLine(motorAdeg + ", " + motorBdeg);
                ev3.moveMotorA(motorAdeg);
                ev3.moveMotorB(motorBdeg);
                Thread.Sleep(5000);
            }
            catch (Exception ex)
            {
                Console.WriteLine(
                    "Oops! We couldn't execute the script because of an exception: " + ex.Message);
            }

            ev3.disconnect ();

            Console.WriteLine("Complete.");
            //			Console.ReadLine();
        }