⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rp6base_tv_remote.c

📁 RP6机器人范例程序。包括移动
💻 C
📖 第 1 页 / 共 2 页
字号:
			movement_command = true;
		break;
		case RC5_KEY_BACKWARDS: 	// Move backwards
			writeString_P("BACKWARDS\n");
			setDefaultSpeedParameters();
			changeDirection(BWD);
			setLEDs(0b001001);
			movement_command = true;
		break;
		case RC5_KEY_STOP: 			// Stop!
			writeString_P("STOP\n");
			max_speed_left = 0;
			max_speed_right = max_speed_left;
			moveAtSpeed(0,0);
			setLEDs(0b011011);
			movement_command = true;
		break;
		case RC5_KEY_CURVE_LEFT: 	// Drive curve left - forwards
			writeString_P("CURVE LEFT FWD\n");
			max_speed_left = MAX_SPEED_CURVE2;
			max_speed_right = MAX_SPEED_CURVE;
			acl_left = ACCELERATE_CURVE2;
			acl_right = ACCELERATE_CURVE;
			decl_left = DECELERATE_CURVE2;
			decl_right = DECELERATE_CURVE;
			changeDirection(FWD);
			setLEDs(0b110100);
			movement_command = true;
		break;
		case RC5_KEY_CURVE_RIGHT: 	// Drive curve right - forwards
			writeString_P("CURVE RIGHT FWD\n");
			max_speed_left = MAX_SPEED_CURVE;
			max_speed_right = MAX_SPEED_CURVE2;
			acl_left = ACCELERATE_CURVE;
			acl_right = ACCELERATE_CURVE2;
			decl_left = DECELERATE_CURVE;
			decl_right = DECELERATE_CURVE2;
			changeDirection(FWD);
			setLEDs(0b100110);
			movement_command = true;
		break;
		case RC5_KEY_CURVE_BACK_LEFT: 	// Drive curve left - backwards
			writeString_P("CURVE LEFT BWD\n");
			max_speed_left = MAX_SPEED_CURVE2;
			max_speed_right = MAX_SPEED_CURVE;
			acl_left = ACCELERATE_CURVE2;
			acl_right = ACCELERATE_CURVE;
			decl_left = DECELERATE_CURVE2;
			decl_right = DECELERATE_CURVE;
			changeDirection(BWD);
			setLEDs(0b011001);
			movement_command = true;
		break;
		case RC5_KEY_CURVE_BACK_RIGHT: 	// Drive curve right - backwards
			writeString_P("CURVE RIGHT BWD\n");
			max_speed_left = MAX_SPEED_CURVE;
			max_speed_right = MAX_SPEED_CURVE2;
			acl_left = ACCELERATE_CURVE;
			acl_right = ACCELERATE_CURVE2;
			decl_left = DECELERATE_CURVE;
			decl_right = DECELERATE_CURVE2;
			changeDirection(BWD);
			setLEDs(0b001011);
			movement_command = true;
		break;
		case RC5_KEY_LEFT_MOTOR_FWD: 	// Only left motor on - forwards
			writeString_P("MOTOR LEFT FWD\n");
			max_speed_left = 0;
			max_speed_right = MAX_SPEED_1_MOTOR;
			acl_left = 4;
			acl_right = 4;
			decl_left = 4;
			decl_right = 4;
			changeDirection(FWD);
			setLEDs(0b110000);
			movement_command = true;
		break;
		case RC5_KEY_LEFT_MOTOR_BWD: 	// Only left motor on - backwards
			writeString_P("MOTOR LEFT BWD\n");
			max_speed_left = 0;
			max_speed_right = MAX_SPEED_1_MOTOR;
			acl_left = 4;
			acl_right = 4;
			decl_left = 4;
			decl_right = 4;
			changeDirection(BWD);
			setLEDs(0b101000);
			movement_command = true;
		break;
		case RC5_KEY_RIGHT_MOTOR_FWD: // Only right motor on - forwards
			writeString_P("MOTOR RIGHT FWD\n");
			max_speed_left = MAX_SPEED_1_MOTOR;
			max_speed_right = 0;
			acl_left = 4;
			acl_right = 4;
			decl_left = 4;
			decl_right = 4;
			changeDirection(FWD);
			setLEDs(0b000110);
			movement_command = true;
		break;
		case RC5_KEY_RIGHT_MOTOR_BWD: 	// Only right motor on - backwards
			writeString_P("MOTOR RIGHT BWD\n");
			max_speed_left = MAX_SPEED_1_MOTOR;
			max_speed_right = 0;
			acl_left = 4;
			acl_right = 4;
			decl_left = 4;
			decl_right = 4;
			changeDirection(BWD);
			setLEDs(0b000101);
			movement_command = true;
		break;
	}
	
	if(movement_command) // Did we receive a move command?
	{
		// Accelerate if neccessary:
		if(getDesSpeedLeft() < max_speed_left) // If we have not reached the left maximum speed...
		{								// ... accelerate!
			moveAtSpeed(getDesSpeedLeft()+acl_left, getDesSpeedRight());
			if(getDesSpeedLeft() < 10)
				moveAtSpeed(10, getDesSpeedRight());
		}
		if(getDesSpeedRight() < max_speed_right) // If we have not reached the right maximum speed...
		{
										// ... accelerate!
			moveAtSpeed(getDesSpeedLeft(), getDesSpeedRight()+acl_right);
			if(getDesSpeedRight() < 10)
				moveAtSpeed(getDesSpeedLeft(), 10);
		}

		// Start Stopwatch 1 - it starts decceleration after 250ms of no RC5 reception! (s. below)
		setStopwatch1(0);
		startStopwatch1();
	}
#endif

}


/*****************************************************************************/

/**
 * This function is called frequently out of the main loop and checks if 
 * Stopwatch1 has counted at least 250ms. If this is the case, decceleration is started
 * and the Stopwatch is resetted and the progtam waits for next 250ms to pass by.
 * Stopwatch1 ist set to 0 and started from the RC5 reception handler after
 * each reception of a valid keycode. (s. above)
 */
void deccelerate(void)
{
	if(getStopwatch1() > 250) // After 250ms with no reception...
	{
		if(getDesSpeedLeft() <= 10) // If left speed is less or equal than 10...
			moveAtSpeed(0, getDesSpeedRight()); // ... stop the left motor
		else				  // Otherwise continue to deccelerate:
			moveAtSpeed(getDesSpeedLeft()-decl_left, getDesSpeedRight());
			
		if(getDesSpeedRight() <= 10) // If right speed is less or equal than 10...
			moveAtSpeed(getDesSpeedLeft(), 0); // ... stop the right motor
		else				  // Otherwise continue to deccelerate:
			moveAtSpeed(getDesSpeedLeft(), getDesSpeedRight()-decl_right);
			
		if (getDesSpeedRight() == 0 && getDesSpeedLeft() == 0)	
			stopStopwatch1(); // Decceleration has finished!
			
		max_speed_left = getDesSpeedLeft(); // Update max_speed value
		max_speed_right = getDesSpeedRight(); // Update max_speed value
		setLEDs(0b000000);			   // and clear LEDs
		setStopwatch1(0);
	}
	
	// Make sure we don't move after Direction has changed and key is released too fast. 
	// This prevents the RP6 from moving when the direction has just changed and temporary saved
	// speed value is written back again in the task_motionControl function.
	if(getDesSpeedLeft() > max_speed_left) 
	{
		if(getDesSpeedLeft() <= 10) // If left speed is less or equal than 10...
			moveAtSpeed(0, getDesSpeedRight()); 		// ... stop the left motor
		else    // decelerate:
			moveAtSpeed(getDesSpeedLeft()-decl_left, getDesSpeedRight()); 
	}
	if(getDesSpeedRight() > max_speed_right) 
	{
		if(getDesSpeedRight() <= 10) // If right speed is less or equal than 10...
				moveAtSpeed(getDesSpeedLeft(), 0); 		// ... stop the right motor
			else    // decelerate:
				moveAtSpeed(getDesSpeedLeft(), getDesSpeedRight()-decl_right); 
	}
}

/*****************************************************************************/
// Main - The program starts here:

int main(void)
{
	initRobotBase(); 
	
	setLEDs(0b111111);
	writeChar('\n');
    writeString_P("RP6 controlled by RC5 TV Remote\n");
	writeString_P("___________________________\n");
	mSleep(500);	 
	setLEDs(0b000000); 
	powerON();
	
	// Set the RC5 Receive Handler:
	IRCOMM_setRC5DataReadyHandler(receiveRC5Data);
	
	//Output small usage instructions and the RC5 Codes:
	writeString_P("\nYou can control your RP6 with the following RC5 Keycodes:");
	writeString_P("\n-----------------------");
	writeString_P("\n * Turn Left: "); 					writeInteger(RC5_KEY_LEFT, DEC);
	writeString_P("\n * Turn Right: "); 				writeInteger(RC5_KEY_RIGHT, DEC);
	writeString_P("\n * Move Forwards: "); 				writeInteger(RC5_KEY_FORWARDS, DEC);
	writeString_P("\n * Move Backwards: "); 			writeInteger(RC5_KEY_BACKWARDS, DEC);
	writeString_P("\n * Stop: "); 						writeInteger(RC5_KEY_STOP, DEC);
	writeString_P("\n * Move curve left forwards: "); 	writeInteger(RC5_KEY_CURVE_LEFT, DEC);
	writeString_P("\n * Move curve right forwards: "); 	writeInteger(RC5_KEY_CURVE_RIGHT, DEC);
	writeString_P("\n * Move curve left backwards: "); 	writeInteger(RC5_KEY_CURVE_BACK_LEFT, DEC);
	writeString_P("\n * Move curve right backwards: "); writeInteger(RC5_KEY_CURVE_BACK_RIGHT, DEC);
	writeString_P("\n * Motor left forwards: "); 		writeInteger(RC5_KEY_LEFT_MOTOR_FWD, DEC);
	writeString_P("\n * Motor left backwards: "); 		writeInteger(RC5_KEY_LEFT_MOTOR_BWD, DEC);
	writeString_P("\n * Motor right forwards: "); 		writeInteger(RC5_KEY_RIGHT_MOTOR_FWD, DEC);
	writeString_P("\n * Motor right backwards: "); 		writeInteger(RC5_KEY_RIGHT_MOTOR_BWD, DEC);
	writeString_P("\n-----------------------\n");
	writeString_P("To change the key mapping, read the comments in the program source code!\n");
	writeString_P("_________\nPlease make sure that your IR Remote Control really transmits RC5 code!\n");
	
	startStopwatch2();
	
	// Main loop 
	while(true) 
	{
		deccelerate(); // Call the deceleration function.
		task_RP6System(); // Motion Control tasks etc.
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -