Ejemplo n.º 1
0
        public static IServiceCollection AddControlLayer(this IServiceCollection services)
        {
            // Configure the Differencial Drive Controller
            services.AddHostedService <DifferentialDriveVelocityController>(s =>
            {
                // Create the motor Hat, and the drivers for left and right motors
                var motorHat         = new MotorHat();
                var leftMotorDriver  = new DCMotorDriver(motorHat.CreateDCMotor(1), s.GetService <ILogger <DCMotorDriver> >());
                var rightMotorDriver = new DCMotorDriver(motorHat.CreateDCMotor(3), s.GetService <ILogger <DCMotorDriver> >());

                // Create the SpeedController that controls left and right motors
                return(new DifferentialDriveVelocityController(
                           leftMotorDriver,
                           rightMotorDriver,
                           s.GetService <IMessageBroker>(),
                           s.GetService <ILogger <DifferentialDriveVelocityController> >()));
            });

            services.Configure <RemoteControlOptions>(options =>
            {
                options.GamepadKeyThrottle = 1;
                options.GamepadKeyYaw      = 0;
            });

            // Configure the Remote Control Controller
            services.AddHostedService <RemoteControlController>(s =>
            {
                var gamepadDriver = new GamepadDriver(s.GetService <ILogger <GamepadDriver> >());

                return(new RemoteControlController(
                           gamepadDriver,
                           s.GetService <IMessageBroker>(),
                           s.GetService <IOptions <RemoteControlOptions> >(),
                           s.GetService <ILogger <RemoteControlController> >()));
            });

            return(services);
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            const double Period = 10.0;
            Stopwatch    sw     = Stopwatch.StartNew();

            // Use the following code to generate an I2C address different from the default
            //var busId = 1;
            //var selectedI2cAddress = 0b000000;     // A5 A4 A3 A2 A1 A0
            //var deviceAddress = MotorHat.I2cAddressBase + selectedI2cAddress;
            //var settings = new I2cConnectionSettings(busId, deviceAddress);

            using (var motorHat = new MotorHat())
            {
                var motor = motorHat.CreateDCMotor(1);

                bool done = false;
                Console.CancelKeyPress += (o, e) =>
                {
                    done     = true;
                    e.Cancel = true;
                };

                string lastSpeedDisp = null;
                while (!done)
                {
                    double time = sw.ElapsedMilliseconds / 1000.0;

                    // Note: range is from -1 .. 1 (for 1 pin setup 0 .. 1)
                    motor.Speed = Math.Sin(2.0 * Math.PI * time / Period);
                    string disp = $"Speed = {motor.Speed:0.00}";
                    if (disp != lastSpeedDisp)
                    {
                        lastSpeedDisp = disp;
                        Console.WriteLine(disp);
                    }

                    Thread.Sleep(1);
                }
            }
        }