Example #1
0
 private void OnUpdateTachoMotorA(object source, System.Timers.ElapsedEventArgs e)
 {
     SpawnThread(delegate()
     {
         MonoBrick.NXT.OutputState state = new MonoBrick.NXT.OutputState();
         bool ok = true;
         int tacho;
         try{
             state = brick.MotorA.GetOutputState();
             tacho = state.RotationCount;
         }
         catch (Exception) {
             ok    = false;
             tacho = lastTachoValueMotorA;
         }
         if (ok && (state.RunState == MonoBrick.NXT.MotorRunState.Idle && lastTachoValueMotorA == tacho) || (state.Speed == 0 && state.RunState == MonoBrick.NXT.MotorRunState.Running))
         {
             motorATachoTimer.Enabled = false;
             //Console.WriteLine("Tacho timer is off");
         }
         lastTachoValueMotorA = tacho;
         Gtk.Application.Invoke(delegate {
             motorATachoEntry.Text = tacho.ToString();
         });
     }, false);
 }
        /// <summary>
        /// Move the motor to a relative position
        /// </summary>
        /// <param name='speed'>
        /// Speed of the motor -100 to 100
        /// </param>
        /// <param name='degrees'>
        /// The relative position of the motor
        /// </param>
        /// <param name='reply'>
        /// If set to <c>true</c> the brick will send a reply
        /// </param>
        public void On(sbyte speed, UInt32 degrees, bool reply)
        {
            OutputState state = new OutputState();

            state.Speed      = speed;
            state.Mode       = MotorMode.Break | MotorMode.On | MotorMode.Regulated;
            state.Regulation = MotorRegulation.Speed;
            state.TurnRatio  = 100;
            state.RunState   = MotorRunState.Running;
            state.TachoLimit = degrees;
            SetOutputState(state, reply);
        }
        /// <summary>
        /// Turn the motor off
        /// </summary>
        /// <param name='reply'>
        /// If set to <c>true</c> the brick will send a reply
        /// </param>
        public void Off(bool reply)
        {
            OutputState state = new OutputState();

            state.Speed      = 0;
            state.Mode       = MotorMode.Break | MotorMode.Regulated;
            state.Regulation = MotorRegulation.Speed;
            state.TurnRatio  = 100;
            state.RunState   = MotorRunState.Running;
            state.TachoLimit = 0;
            SetOutputState(state, reply);
        }
        /// <summary>
        /// Brake the vehicle (the motor is still on but it does not move)
        /// </summary>
        /// <param name='reply'>
        /// If set to <c>true</c> the brick will send a reply
        /// </param>
        public void Brake(bool reply)
        {
            OutputState state = new OutputState();

            state.Speed = 0;
            state.Mode  = MotorMode.Break | MotorMode.On | MotorMode.Regulated;
            if (Sync)
            {
                state.Regulation = MotorRegulation.Sync;
            }
            else
            {
                state.Regulation = MotorRegulation.Speed;
            }
            state.TurnRatio  = 100;
            state.RunState   = MotorRunState.Running;
            state.TachoLimit = 0;
            left.SetOutputState(state, reply);
            right.SetOutputState(state, reply);
        }
        /*public void Forward(sbyte speed, UInt32 degrees){
         *      Forward(speed,degrees,false);
         * }
         * public void Forward(sbyte speed, UInt32 degrees, bool reply){
         *      Move(speed, degrees, reply);
         * }*/

        private void Move(sbyte speed, UInt32 degrees, bool reply)
        {
            OutputState state = new OutputState();

            state.Speed = speed;
            state.Mode  = MotorMode.Break | MotorMode.On | MotorMode.Regulated;
            if (Sync)
            {
                state.Regulation = MotorRegulation.Sync;
            }
            else
            {
                state.Regulation = MotorRegulation.Speed;
            }
            state.TurnRatio  = 100;
            state.RunState   = MotorRunState.Running;
            state.TachoLimit = degrees;
            left.SetOutputState(state, reply);
            right.SetOutputState(state, reply);
        }
        /// <summary>
        /// Gets the output state of the motor
        /// </summary>
        /// <returns>
        /// The output state
        /// </returns>
        public OutputState GetOutputState()
        {
            OutputState motorOutput = new OutputState();
            var         command     = new Command(CommandType.DirecCommand, CommandByte.GetOutputState, true);

            command.Append((byte)port);
            connection.Send(command);
            var reply = connection.Receive();

            Error.CheckForError(reply, 25);
            motorOutput.Speed           = reply.GetSbyte(4);
            motorOutput.Mode            = (MotorMode)reply[5];
            motorOutput.Regulation      = (MotorRegulation)reply[6];
            motorOutput.TurnRatio       = reply.GetSbyte(7);
            motorOutput.RunState        = (MotorRunState)reply[8];
            motorOutput.TachoLimit      = reply.GetUInt32(9);
            motorOutput.TachoCount      = reply.GetInt32(13);
            motorOutput.BlockTachoCount = reply.GetInt32(17);
            motorOutput.RotationCount   = reply.GetInt32(21);
            return(motorOutput);
        }
        /// <summary>
        /// Sets the output state of the motor
        /// </summary>
        /// <param name='state'>
        /// Outputstate
        /// </param>
        /// <param name='reply'>
        /// If set to <c>true</c> the brick will send a reply
        /// </param>
        public void  SetOutputState(OutputState state, bool reply)
        {
            if (state.Speed > 100)
            {
                state.Speed = 100;
            }
            if (state.Speed < -100)
            {
                state.Speed = -100;
            }
            if (state.TurnRatio > 100)
            {
                state.TurnRatio = 100;
            }
            if (state.TurnRatio < -100)
            {
                state.TurnRatio = 100;
            }
            if (Reverse)
            {
                state.Speed = (sbyte)-state.Speed;
            }
            var command = new Command(CommandType.DirecCommand, CommandByte.SetOutputState, reply);

            command.Append((byte)port);
            command.Append(state.Speed);
            command.Append((byte)state.Mode);
            command.Append((byte)state.Regulation);
            command.Append(state.TurnRatio);
            command.Append((byte)state.RunState);
            command.Append(state.TachoLimit);
            command.Append((byte)0x00);             //why a 5th byte?
            connection.Send(command);
            if (reply)
            {
                var brickReply = connection.Receive();
                Error.CheckForError(brickReply, 3);
            }
        }
	private void OnUpdateTachoMotorA(object source, System.Timers.ElapsedEventArgs e){
		SpawnThread(delegate()
        {
			MonoBrick.NXT.OutputState	state = new MonoBrick.NXT.OutputState();
			bool ok = true;
			int tacho;
			try{
				state = brick.MotorA.GetOutputState();
				tacho = state.RotationCount;
			}
			catch(Exception){
				ok = false;
				tacho = lastTachoValueMotorA;
			}
			if(ok && (state.RunState == MonoBrick.NXT.MotorRunState.Idle && lastTachoValueMotorA == tacho) || (state.Speed == 0 && state.RunState == MonoBrick.NXT.MotorRunState.Running)){
				motorATachoTimer.Enabled = false;
				//Console.WriteLine("Tacho timer is off");
			}
			lastTachoValueMotorA = tacho;
			Gtk.Application.Invoke (delegate {
				motorATachoEntry.Text = tacho.ToString();	
			});
		},false);
	}
 /// <summary>
 /// Sets the output state of the motor
 /// </summary>
 /// <param name='state'>
 /// Outputstate
 /// </param>
 public void  SetOutputState(OutputState state)
 {
     SetOutputState(state, false);
 }
		/*public void Forward(sbyte speed, UInt32 degrees){
			Forward(speed,degrees,false);
		}
		public void Forward(sbyte speed, UInt32 degrees, bool reply){
			Move(speed, degrees, reply);
		}*/

		private void Move(sbyte speed, UInt32 degrees, bool reply){
			OutputState state = new OutputState();
			state.Speed = speed;
			state.Mode = MotorMode.Break | MotorMode.On | MotorMode.Regulated;
			if(Sync)
				state.Regulation =  MotorRegulation.Sync;
			else{
				state.Regulation =  MotorRegulation.Speed;
			}
			state.TurnRatio = 100;
			state.RunState = MotorRunState.Running;
			state.TachoLimit = degrees;
			left.SetOutputState(state,reply);
			right.SetOutputState(state,reply);
		}
		/// <summary>
		/// Brake the vehicle (the motor is still on but it does not move)
		/// </summary>
		/// <param name='reply'>
		/// If set to <c>true</c> the brick will send a reply
		/// </param>
		public void Brake(bool reply){
			OutputState state = new OutputState();
			state.Speed = 0;
			state.Mode = MotorMode.Break | MotorMode.On | MotorMode.Regulated;
			if(Sync)
				state.Regulation =  MotorRegulation.Sync;
			else{
				state.Regulation =  MotorRegulation.Speed;
			}
			state.TurnRatio = 100;
			state.RunState = MotorRunState.Running;
			state.TachoLimit = 0;
			left.SetOutputState(state,reply);
			right.SetOutputState(state,reply);
		}
		/// <summary>
		/// Gets the output state of the motor
		/// </summary>
		/// <returns>
		/// The output state
		/// </returns>
        public OutputState GetOutputState() {
            OutputState motorOutput = new OutputState();
			var command = new Command(CommandType.DirecCommand, CommandByte.GetOutputState,true);
			command.Append((byte)port);
			connection.Send(command);
			var reply = connection.Receive();
			Error.CheckForError(reply,25);
			motorOutput.Speed = reply.GetSbyte(4);
            motorOutput.Mode = (MotorMode)reply[5];
            motorOutput.Regulation = (MotorRegulation)reply[6];
            motorOutput.TurnRatio = reply.GetSbyte(7);
            motorOutput.RunState = (MotorRunState)reply[8];
            motorOutput.TachoLimit = reply.GetUInt32(9);
            motorOutput.TachoCount = reply.GetInt32(13);
            motorOutput.BlockTachoCount = reply.GetInt32(17);
            motorOutput.RotationCount = reply.GetInt32(21);
            return motorOutput;
        }
		/// <summary>
		/// Turn the motor off
		/// </summary>
		/// <param name='reply'>
		/// If set to <c>true</c> the brick will send a reply
		/// </param>
        public void Off(bool reply)
        {
            OutputState state = new OutputState();
			state.Speed = 0;
			state.Mode = MotorMode.Break | MotorMode.Regulated;
			state.Regulation =  MotorRegulation.Speed;
			state.TurnRatio = 100;
			state.RunState = MotorRunState.Running;
			state.TachoLimit = 0;
			SetOutputState(state, reply);
        }
		/// <summary>
		/// Move the motor to a relative position
		/// </summary>
		/// <param name='speed'>
		/// Speed of the motor -100 to 100
		/// </param>
		/// <param name='degrees'>
		/// The relative position of the motor
		/// </param>
		/// <param name='reply'>
		/// If set to <c>true</c> the brick will send a reply
		/// </param>
		public void On(sbyte speed, UInt32 degrees,bool reply){
            OutputState state = new OutputState();
			state.Speed = speed;
			state.Mode = MotorMode.Break | MotorMode.On | MotorMode.Regulated;
			state.Regulation =  MotorRegulation.Speed;
			state.TurnRatio = 100;
			state.RunState = MotorRunState.Running;
			state.TachoLimit = degrees;
			SetOutputState(state, reply);  
		}
		/// <summary>
		/// Sets the output state of the motor
		/// </summary>
		/// <param name='state'>
		/// Outputstate
		/// </param>
		/// <param name='reply'>
		/// If set to <c>true</c> the brick will send a reply
		/// </param>
		public void  SetOutputState(OutputState state, bool reply){
			if(state.Speed > 100){
				 state.Speed = 100;
			}
			if(state.Speed < -100){
				state.Speed = -100;
			}
			if(state.TurnRatio > 100){
				state.TurnRatio = 100;
			}
			if(state.TurnRatio < -100){
				state.TurnRatio = 100;
			}
			if (Reverse)
            	state.Speed = (sbyte)-state.Speed;
            var command = new Command(CommandType.DirecCommand,CommandByte.SetOutputState, reply);
			command.Append((byte)port);
			command.Append(state.Speed);
			command.Append((byte)state.Mode);
			command.Append((byte)state.Regulation);
            command.Append(state.TurnRatio);
			command.Append((byte)state.RunState);
			command.Append(state.TachoLimit);
			command.Append((byte) 0x00);//why a 5th byte?
			connection.Send(command);
			if(reply){
				var brickReply = connection.Receive();
				Error.CheckForError(brickReply,3);
			}
		}
		/// <summary>
		/// Sets the output state of the motor
		/// </summary>
		/// <param name='state'>
		/// Outputstate
		/// </param>
		public void  SetOutputState(OutputState state){
			SetOutputState(state,false);
		}