Fjernstyret Mindstorm Pick-up truck!

 

 

Funktioner

• Fjernbetjent med sekundær NXT via BlueTooth. Frem, Stop, Bak, Dreje, Brems, Auto opretning (ved brems i bunden)

• Gradvis acceleration for at skåne gearkassen.

• Autostop af motor ved kontakt med forhindringer. Dvs. ingen gradvis nedbremsning som ved alm stop.

 

 

Pickup.

DSC_0045DSC_0046DSC_0047DSC_0048DSC_0049DSC_0050

Kode til Pickup.

#pragma config(Sensor, S1,     forh,           sensorTouch)
#pragma config(Sensor, S2,     bagv,           sensorTouch)
#pragma config(Sensor, S3,     bagh,           sensorTouch)
#pragma config(Sensor, S4,     forv,           sensorTouch)
#pragma config(Motor,  motorA,          motor1,        tmotorNXT, openLoop, encoder)
#pragma config(Motor,  motorB,          motor2,        tmotorNXT, openLoop, encoder)
#pragma config(Motor,  motorC,          styring,       tmotorNXT, openLoop, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int targetPower;
int displayInfo;
int motorSpeed;
int targetDegreesL = 95;
int targetDegreesR = -95;
int steerTarget;
bool changeNow = false;
string move;
bool brake;

task steering()
{
	while(true)
	{
		if (steerTarget == targetDegreesR)  // Right
		{
			while (nMotorEncoder[motorC] > steerTarget )
			{
				motor[motorC] = -40;
				if (steerTarget != targetDegreesR) break;
			}
			motor[motorC] = 0;
		}
		if (steerTarget == targetDegreesL)	// Left
		{
			while(nMotorEncoder[motorC] < steerTarget )
			{
				motor[motorC] = 40;
				if (steerTarget != targetDegreesL) break;
			}
			motor[motorC] = 0;
		}
		if (steerTarget == 0) // Center
		{
			if (nMotorEncoder[motorC] > (steerTarget+7) )  
			{
				while(nMotorEncoder[motorC] > (steerTarget+3) )
				{
					motor[motorC] = -40;
					if (steerTarget != 0) break;
				}
				motor[motorC] = 0;
			}
			if (nMotorEncoder[motorC] < (steerTarget-7) )
			{
				while ((nMotorEncoder[motorC] < steerTarget-3) )
				{
					motor[motorC] = 40;
					if (steerTarget != 0) break;
				}
				motor[motorC] = 0;
			}
		}
		wait1Msec(50);
	}
}

task setPower()
{
	while(true)
	{
		//motorSpeed = motorPWMLevel[motor1];
		motorSpeed = motor[motor1];

		if (brake == true)
		{
			motor[motor1] = 0;
			motor[motor2] = 0;
		}
		else
		{
			if(motorSpeed < targetPower)
			{
				motorSpeed ++;
				//wait1Msec(50);
				motor[motor1] = motorSpeed;
				motor[motor2] = motorSpeed;
				displayInfo = motorSpeed;
			}

			if(motorSpeed > targetPower)
			{
				motorSpeed --;
				//wait1Msec(50);
				motor[motor1] = motorSpeed;
				motor[motor2] = motorSpeed;
				displayInfo = motorSpeed;
			}
		}
		wait1Msec(20);
	}
}

task main()
{
	startTask(setPower);
	startTask(steering);
	nMotorEncoder[styring] = 0;

	ClearMessage();

	while(true)
	{
		if (message == 04) // Stop
		{
			targetPower = 0;
			steerTarget = 0;
			wait1Msec (50);
		}
		if (message == 30) // frem
		{
			targetPower = 75;
			wait1Msec (50); // sikre at bt besked nr at blive laest
			brake = false;
		}
		if (message == 03) // tilbage
		{
			targetPower = -75;
			wait1Msec (50);
			brake = false;
		}
		if (message == 21) // dreje hoejre
		{
			steerTarget = targetDegreesL;
			wait1Msec (50);
		}
		if (message == 12) // dreje venstre
		{
			steerTarget = targetDegreesR;
			wait1Msec (50);
		}
		ClearMessage();
		if ((SensorValue[forv] == 1) || (SensorValue[forh] == 1) || (SensorValue[bagv] == 1) || (SensorValue[bagh]  == 1))
		{
			targetPower = 0;
			steerTarget = 0;
			brake = true;
		}
	}
}

 

Fjernbetjening.

DSC_0043DSC_0044

Kode til fjernbetjening.

#pragma config(Sensor, S4,     Bremser,        sensorTouch)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

int Hojre = 12;
int Venstre = 21;
int Frem = 30;
int Bak = 03;
int Brems = 04;

string besked;

task display()
{
	while(true)  // infinite loop:
	{
		nxtDisplayCenteredBigTextLine(3, besked   );
		wait1Msec(100);  // Wait 100 milliseconds to help display correctly
	}
}

task main()
{
	startTask(display);

	while(true)
	{
		nNxtExitClicks = 5;

		if(SensorValue[S4] == 1)
		{
			sendMessage(Brems);
			besked = "Brems";
		}
		if(nNxtButtonPressed == 1)
		{
			sendMessage(Hojre);
			besked = "Hojre";
		}
		if(nNxtButtonPressed == 2)
		{
			sendMessage(Venstre);
			besked = "Venstre";
		}
		if(nNxtButtonPressed == 3)
		{
			sendMessage(Frem);
			besked = "Frem";
		}
		if(nNxtButtonPressed == 0)
		{
			sendMessage(Bak);
			besked = "Bak";
		}
	}
}

 

 

 

Skriv et svar