void main(void) {
	WDTCTL = WDTPW + WDTHOLD; //disable watchdog
	
	//int dir1=1;		// Direction for PWM duty cycle change. A +1
					// value increases, a -1 value decreases
	//int dir2=-1;    //start direction for number 2
	
	//initialization stuff here
		//remember to enable any pull-up/downs!
		
	P1OUT = 0;
	P1DIR |= ALL_LEDS;
	P1SEL |= (LED2 | LED1); //initialize the TA0.1 on led2,
					//and TA0.2 on led1
	P1SEL2 |= LED1;
							//P1SEL2 is important here!
	
	BCSCTL1 = CALBC1_1MHZ;          // Running at 1 MHz
    DCOCTL = CALDCO_1MHZ;
    
    TA_init();
	
	ADC_init();
	
	//infinite loop
	for(;;) {
		/*__delay_cycles(PWM_TOP*5);
		TACCR1 += dir1;
		TACCR2 += dir2;
		if (TACCR1 == PWM_TOP){
			dir1 = -1; //switch directions
			dir2 = 1;
		} else if (TACCR1 == 0){
			dir1 = 1;
			dir2 = -1;
		}*/
		
		ADC_read_vals();
		set_pwms();
		//TACCR1 = 5;
		
	}
	//return 0; //should never reach this	
}
Exemple #2
0
int main(void) {
	// UDP Creation

    struct sockaddr_in si_me, si_other;

    int s, slen = sizeof(si_other) , recv_len;
    char buf[BUFLEN];
    char motor1_2, motor1_1,motor2_2, motor2_1,motor3_2, motor3_1,motor4_2, motor4_1,motor5_2, motor5_1,motor6_2, motor6_1;
    char motor1[3], motor2[3], motor3[3], motor4[3], motor5[3], motor6[3];

    //create a UDP socket
    if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
    {
        die("socket");
    }

    // zero out the structure
    memset((char *) &si_me, 0, sizeof(si_me));

    si_me.sin_family = AF_INET;
    si_me.sin_port = htons(PORT);
    si_me.sin_addr.s_addr = htonl(INADDR_ANY);

    //bind socket to port
    if( bind(s , (struct sockaddr*)&si_me, sizeof(si_me) ) == -1)
    {
        die("bind");
    }

    //Set up pwms
    char freq[5];
    int i, motor_num;
	set_pwms();
	//Ask the user what the frequency should be
	printf("What should the frequency be? ");
	fflush(stdout);
	scanf("%s", freq);
	//Set all frequencies to the user input
	for(i='1';i<'7';i++){
			set_freq(i,freq);
	}
	//Activate the run file in each pwm folder
	for(i='1';i<'7';i++){
			set_run(i,"1");
	}
	//Set the duty percent to 50 for all pwms
	for(i='1';i<'7';i++){
			set_duty(i,"50");
	}
	while(1){

        //try to receive some data
        if ((recv_len = recvfrom(s, buf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen)) == -1)
        {
            die("recvfrom()");
        }

        // Figure out the number of motors from first three bytes of the array received from UDP packet
        motor_num = buf[3];
        int motors[motor_num];
        // Create an array that has the duty percent for each motor in the elements
        for(i=0; i<motor_num; i++){
        	motors[i] = buf[i+4];
        	if(motors[i] > 100) motors[i] = motors[i] - 256; //Conversion for numbers that should be negative
        }
        fflush(stdout);

        //Change the percentages from -100-100 to 0-100 because that's the percentages the BeagleBone can do
        for(i=0; i<6; i++){
        	motors[i] = (motors[i] + 100)/2;
        }

        // The next bunch of if else statements convert the integer duty percent number into strings that the
        // Beaglebone can understand.  Each statement will go through the first and second digit of the integer
        // and then change them to ASCII code and put them into an array.
        if(motors[0] == 100){
        	motor1[0] = '1';
        	motor1[1] = '0';
        	motor1[2] = '0';
        	motor1[3] = '\0';
        }
        else {
            motor1_2 = motors[0]%10 + 0x30;
            motor1_1 = (motors[0]/10)%10 + 0x30;
            motor1[0] = '0';
            motor1[1] = motor1_1;
            motor1[2] = motor1_2;
            motor1[3] = '\0';
        }
        if(motors[1] == 100){
        	motor2[0] = '1';
        	motor2[1] = '0';
        	motor2[2] = '0';
            motor2[3] = '\0';
        }
        else {
            motor2_2 = motors[1]%10 + 0x30;
            motor2_1 = (motors[1]/10)%10 + 0x30;
            motor2[0] = '0';
            motor2[1] = motor2_1;
            motor2[2] = motor2_2;
            motor2[3] = '\0';
        }
        if(motors[2] == 100){
        	motor3[0] = '1';
        	motor3[1] = '0';
        	motor3[2] = '0';
            motor3[3] = '\0';
        }
        else {
            motor3_2 = motors[2]%10 + 0x30;
            motor3_1 = (motors[2]/10)%10 + 0x30;
            motor3[0] = '0';
            motor3[1] = motor3_1;
            motor3[2] = motor3_2;
            motor3[3] = '\0';
        }
        if(motors[3] == 100){
        	motor4[0] = '1';
        	motor4[1] = '0';
        	motor4[2] = '0';
        	motor4[3] = '\0';
        }
        else {
            motor4_2 = motors[3]%10 + 0x30;
            motor4_1 = (motors[3]/10)%10 + 0x30;
            motor4[0] = '0';
            motor4[1] = motor4_1;
            motor4[2] = motor4_2;
            motor4[3] = '\0';
        }
        if(motors[4] == 100){
        	motor5[0] = '1';
        	motor5[1] = '0';
        	motor5[2] = '0';
            motor5[3] = '\0';
        }
        else {
            motor5_2 = motors[4]%10 + 0x30;
            motor5_1 = (motors[4]/10)%10 + 0x30;
            motor5[0] = '0';
            motor5[1] = motor5_1;
            motor5[2] = motor5_2;
            motor5[3] = '\0';
        }
        if(motors[5] == 100){
        	motor6[0] = '1';
        	motor6[1] = '0';
        	motor6[2] = '0';
            motor6[3] = '\0';
        }
        else {
            motor6_2 = motors[5]%10 + 0x30;
            motor6_1 = (motors[5]/10)%10 + 0x30;
            motor6[0] = '0';
            motor6[1] = motor6_1;
            motor6[2] = motor6_2;
            motor6[3] = '\0';
        }

        // Print all the duty percents that were calculated
        printf("First Motor: %s\n", motor1);
        printf("Second Motor: %s\n", motor2);
        printf("Third Motor: %s\n", motor3);
        printf("Fourth Motor: %s\n", motor4);
        printf("Fifth Motor: %s\n", motor5);
        printf("Sixth Motor: %s\n\n", motor6);

        // Set the duty percent of each motor
    	set_duty('1', motor1);
    	set_duty('2', motor2);
    	set_duty('3', motor3);
    	set_duty('4', motor4);
    	set_duty('5', motor5);
    	set_duty('6', motor6);

	}

	close(s);
	return 0;
}