Beispiel #1
0
int main(void)
{
	int data;
	if(wiringPiSetup () == -1)
		return 1;

	pinMode(M1_A, OUTPUT);
	pinMode(M1_B, OUTPUT);
	pinMode(M2_A, OUTPUT);
	pinMode(M2_B, OUTPUT);

	while(1)
	{
		printf("Press Key, you can I:Forward, J:Left, L:Right, ,:Rear, K:Stop\n");
		scanf("%c", &data);
		if(data == ',')
		{
			printf("Rear\n");
			runMotor( Motor1, Dir_Forward );
                        runMotor( Motor2, Dir_Forward );
		}
		else if(data == 'i' || data == 'I')
		{
			printf("Front\n");
			runMotor( Motor1, Dir_Backward );
                        runMotor( Motor2, Dir_Backward );
		}
		else if(data == 'j' || data == 'J')
		{
			printf("Left\n");
			runMotor( Motor1, Dir_Forward );
		}
		else if(data == 'l' || data == 'L')
		{
			printf("Right\n");
			runMotor( Motor2, Dir_Forward );
		}
		else if(data == 'k' || data == 'K')
		{
			printf("Stoped\n");
			runMotor( Motor1, Dir_STOP );
			runMotor( Motor2, Dir_STOP );
		}
	}
	return 0;
}
void main( void ) {
	 SPI1CR1=0x00;
	
	//initialize LCD, RTI,KISR,pac_isr,and ATDo interrupt
	Lcd2PP_Init(); 			
	runKeypad();
	runMotor();
	runTemperature();
	runRTI();
	 
	//activate interrupt 
	INTR_ON(); 

	//infinite loop to run program forever
	for (;;){
	
			}//end for loop

}//end main
int main(){

	printf("starting\n");

	//Setting up library

	wiringPiSetup();

	//Initializing motor hat

	word i2c = init();

	//Sets the steering speed to fast
	setSpeed(i2c, 4, 250);
	word condition = 1;


	//Declaring sockets

	int server_sock_fd = 0, client_sock_fd = 0;

	struct sockaddr_in server_address;

	int iSetOption = 1;


	//Creating main socket wth ability to reuse port address

	server_sock_fd = socket(AF_INET, SOCK_STREAM, 0);

	setsockopt(server_sock_fd, SOL_SOCKET, SO_REUSEADDR, (char*)&iSetOption, sizeof(iSetOption));

	if (server_sock_fd == -1) {

		perror("socket");

		return EXIT_FAILURE;

	}



	//Setting server address to all 0's so that it is clean

	memset(&server_address, '0', sizeof(server_address));

	//Setting server address to needed values such as port number
	server_address.sin_family = AF_INET;

	server_address.sin_addr.s_addr = htonl(INADDR_ANY);

	server_address.sin_port = htons(51717);


	printf("socket made\n");


	//Binding socket to port

	if (-1 == bind(server_sock_fd, (struct sockaddr*)&server_address, sizeof(server_address))) {

		perror("bind");
		return EXIT_FAILURE;

	}


	printf("socket bind\n");



	//Listening for connecton from android

	if (-1 == listen(server_sock_fd, 1)) {

	perror("listen");

	printf("listen error\n");

	return EXIT_FAILURE;

	}


	//Declaring varibale to store recived data
	char data;


	//Label for goto statement
	reconnect:


	//Setting forward/backward speed to slow to match android default

	setSpeed(i2c, 2, 150);

	//Accepting connection from android

	client_sock_fd = accept(server_sock_fd, (struct sockaddr*)NULL, NULL);


	printf("connected\n");


	//Entering loop to receive and process data from android

	while(condition == 1) {

		//Declaring variables to handle connecton loss

		int error = 0;

		socklen_t len = sizeof(error);
		int retval = getsockopt(client_sock_fd, SOL_SOCKET, SO_ERROR, &error, &len);

		//If connection is lost redirect to listen for a new connection
		if (retval != 0 || error != 0) {

			goto reconnect;
		}

		//Receive data from android

		recv(client_sock_fd, &data, 1, 0);



		//Parse data from android to integer

		int dataPass = data - '0';


		//Process data from android

		processData(i2c, dataPass);



		printf("%c\n", data);

	
			
		//If android app is closed, redrect to listen for new connection and release motors

		if (dataPass == 8) {

			close(client_sock_fd);

			runMotor(i2c, 1, MOTOR_RELEASE);

			runMotor(i2c, 4, MOTOR_RELEASE);

			goto reconnect;

		}

	}
	
	return 0;

}
void processData(word i2c, int data){

	word motor;



	//Controls motor based on data received from android

	switch(data){

		case 1:

			motor = 1;

			runMotor(i2c, motor, MOTOR_FORWARD);

			break;

		case 2:

			motor = 1;

			//TURN OFF MOTOR

			runMotor(i2c, motor, MOTOR_RELEASE);

			break;

		case 3:

			motor = 1;

			runMotor(i2c, motor, MOTOR_BACK);

			break;

		case 4:

			motor = 4;

			runMotor(i2c, motor, MOTOR_FORWARD);

			break;

		case 5:

			motor = 4;

			//TURN OFF MOTOR

			runMotor(i2c, motor, MOTOR_RELEASE);

			break;

		case 6:

			motor = 4;

			runMotor(i2c, motor, MOTOR_BACK);

			break;


			//Sets speed of forward and backward motion

		case 7:

			motor = 1;

			//FAST SPEED

			setSpeed(i2c, motor, 250);

			break;

		case 9:

			motor = 1;

			//MEDIM SPEED

			setSpeed(i2c, motor, 200);

			break;

		case 0:

			motor = 1;

			//SLOW SPEED

			setSpeed(i2c ,motor, 150);

			break;

		//Releases motors if invalid data is received

		default:

			runMotor(i2c, 1, MOTOR_RELEASE);

			runMotor(i2c, 4, MOTOR_RELEASE);

			break;

	}

}