Я могу успешно фиксировать изменения состояния кнопок.
Проблема в том, что я не могу получить доступ к сервопеременной (
Код: Выделить всё
deviceЯ пробовал добавить статические значения в переменную.
Код: Выделить всё
using System.Threading;
using System.Diagnostics;
using GHIElectronics.TinyCLR.Pins;
using GHIElectronics.TinyCLR.Drivers.Infrared;
using GHIElectronics.TinyCLR.Devices.Gpio;
using System;
using GHIElectronics.TinyCLR.UI.Controls;
using GHIElectronics.TinyCLR.Devices.I2c;
namespace RobotApp
{
class Program
{
static int buttonPressed = 100;
static bool isPoweredOn;
static void Main(string[] args)
{
//IR Remote Setup
var recievePin = GpioController.GetDefault().OpenPin(FEZBit.GpioPin.P8);
var ir = new NecIRDecoder(recievePin);
ir.OnDataReceivedEvent += Ir_OnDataRecievedEvent;
ir.OnRepeatEvent += Ir_OnRepeatEvent;
Debug.WriteLine("System is ready!");
//Setup for servos
var settings = new I2cConnectionSettings(0x01, 100_000);
var controller = I2cController.FromName(FEZBit.I2cBus.Edge);
var device = controller.GetDevice(settings);
while (true)
{
if (buttonPressed == 0)
{
isPoweredOn = !isPoweredOn;
Debug.WriteLine("Power Button pressed!");
TurnOnRobot(isPoweredOn);
buttonPressed = 100;
}
}
}
//Events for button presses
static void Ir_OnDataRecievedEvent(byte address, byte command)
{
Debug.WriteLine("A: " + address + " C: " + command);
buttonPressed = command;
}
static void Ir_OnRepeatEvent()
{
Debug.WriteLine("Repeat!");
}
//Turning the robot on with remote by bool
static void TurnOnRobot(bool value)
{
//Turn on or off
if(value)
Debug.WriteLine("Turned robot on!");
else
{
Debug.WriteLine("Turned robot off!");
}
}
//Sets servo motor speed
static void SetMotorSpeed(double left, double right)
{
var b5 = new byte[5];
left = left / 100; right = right / 100;
b5[0] = 0x02;
if (left > 0)
{
b5[1] = (byte)(left * 255);
b5[2] = 0x00;
}
else
{
left *= -1;
b5[1] = 0x00;
b5[2] = (byte)(left * 255);
}
if (right > 0)
{
b5[3] = (byte)(right * 255);
b5[4] = 0x00;
}
else
{
right *= -1;
b5[3] = 0x00;
b5[4] = (byte)(right * 255);
}
device.Write(b5);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/786 ... her-method