/// <summary> /// Create a vehicule with 2 motors, one left and one right /// </summary> /// <param name="left">Motor port for left motor</param> /// <param name="right">Motor port for right motor</param> public Vehicule(BrickPortMotor left, BrickPortMotor right) { brick = new Brick(); //brick.Start(); portleft = left; portright = right; }
/// <summary> /// Create a motor /// </summary> /// <param name="brick">The brick controlling the motor</param> /// <param name="port">Motor port</param> /// <param name="timeout">Timeout</param> public Motor(Brick brick, BrickPortMotor port, int timeout) { _brick = brick; Port = port; _periodRefresh = timeout; _timer = new Timer(UpdateSensor, this, TimeSpan.FromMilliseconds(timeout), TimeSpan.FromMilliseconds(timeout)); }
/// <summary> /// Create a vehicule with 2 motors, one left and one right /// </summary> /// <param name="left">Motor port for left motor</param> /// <param name="right">Motor port for right motor</param> public Vehicule(Brick brick, BrickPortMotor left, BrickPortMotor right) { this.brick = brick; //brick.Start(); portleft = left; portright = right; }
public Motor(BrickPortMotor port, int timeout) { brick = new Brick(); Port = port; //brick.Start(); periodRefresh = timeout; timer = new Timer(UpdateSensor, this, TimeSpan.FromMilliseconds(timeout), TimeSpan.FromMilliseconds(timeout)); }
private bool IsRunning(BrickPortMotor port) { if (brick.BrickPi.Motor[(int)port].Enable == 0) { return(false); } return(true); }
private bool IsRunning(BrickPortMotor port) { //if (brick.BrickPi.Motor[(int)port].Enable == 0) try { if (_brick.GetMotorStatus((byte)port).Speed == 0) { return(false); } } catch (Exception) { } return(true); }
/// <summary> /// Create a motor /// </summary> /// <param name="brick">The brick controlling the motor</param> /// <param name="port">Motor port</param> public Motor(Brick brick, BrickPortMotor port) : this(brick, port, 1000) { }
/// <summary> /// Create a vehicule with 2 motors, one left and one right /// </summary> /// <param name="brick">The brick controlling the motor</param> /// <param name="left">Motor port for left motor</param> /// <param name="right">Motor port for right motor</param> public Vehicule(Brick brick, BrickPortMotor left, BrickPortMotor right) { this._brick = brick; PortLeft = left; PortRight = right; }
/// <summary> /// Create a motor /// </summary> /// <param name="port">Motor port</param> public Motor(BrickPortMotor port) : this(port, 1000) { }
private bool IsRunning(BrickPortMotor port) { if (brick.BrickPi.Motor[(int)port].Enable == 0) return false; return true; }
private async Task RunMotorSyncDegrees(BrickPortMotor[] ports, int[] speeds, int[] degrees) { if ((ports == null) || (speeds == null) || degrees == null) return; if ((ports.Length != speeds.Length) && (degrees.Length != speeds.Length)) return; //make sure we have only positive degrees for (int i = 0; i < degrees.Length; i++) if (degrees[i] < 0) degrees[i] = -degrees[i]; //initialize the speed and enable motors int[] initval = new int[ports.Length]; for (int i = 0; i < ports.Length; i++) { initval[i] = brick.BrickPi.Motor[(int)ports[i]].Encoder; StartMotor((int)ports[i], speeds[i]); } bool nonstop = true; while(nonstop) { bool status = false; for (int i = 0; i < ports.Length; i++) { if (speeds[i] > 0) { if (brick.BrickPi.Motor[(int)ports[i]].Encoder >= (initval[i] + degrees[i] * 2)) { StopMotor((int)ports[i]); } } else { if (brick.BrickPi.Motor[(int)ports[i]].Encoder <= (initval[i] - degrees[i] * 2)) { StopMotor((int)ports[i]); } } status |= IsRunning(ports[i]); } nonstop = status; } }
private async Task RunMotorSyncTime(BrickPortMotor[] ports, int[] speeds, int timeout) { if ((ports == null) || (speeds == null)) return; if (ports.Length != speeds.Length) return; //create a timer for the needed time to run if (timer == null) timer = new Timer(RunUntil, ports, TimeSpan.FromMilliseconds(timeout), Timeout.InfiniteTimeSpan); else timer.Change(TimeSpan.FromMilliseconds(timeout), Timeout.InfiniteTimeSpan); //initialize the speed and enable motors for(int i=0; i<ports.Length; i++) { StartMotor((int)ports[i], speeds[i]); } bool nonstop = true; while(nonstop) { bool status = false; for (int i=0; i<ports.Length;i++) { status |= IsRunning(ports[i]); } nonstop = status; } }