Пример #1
1
int main(){
	signal(SIGINT, &sig_handler);

	uint16_t rotary_value = 0;
	float value = 0.0f;
	mraa_pwm_context pwm1;
	mraa_aio_context rotary;

	pwm1 = mraa_pwm_init(5);
	rotary = mraa_aio_init(0);

	if (rotary == NULL || pwm1 == NULL) {
		return 1;
		printf("Broken\n");
	 }

	mraa_pwm_period_ms(pwm1, 20);
	mraa_pwm_enable(pwm1, 1);


	while(isrunning){
		rotary_value = mraa_aio_read(rotary);
		//convert to 0.00 to 1.00 scale
		value = ((float) rotary_value)/102;

		//convert to 0.025 to 0.1 scale (avoid rattle)
		value = value/13.33;
		value = value + 0.025;
 if (value >= .25){
	 	mraa_pwm_enable(pwm1, 1);
		printf("%f\n", value);
		mraa_pwm_write(pwm1, value);
 }
 else{
//		mraa_pwm_write(pwm1, 0);
		mraa_pwm_enable(pwm1, 0);

	}
	}
		mraa_pwm_write(pwm1, 0.025f);
		mraa_pwm_enable(pwm1, 0);
	return 0;
}
Пример #2
0
int main()
{

mraa_gpio_context in1;
mraa_gpio_context in2;
mraa_gpio_context in3;
mraa_gpio_context in4;

in1=mraa_gpio_init(7);
in2=mraa_gpio_init(8);
in3=mraa_gpio_init(9);
in4=mraa_gpio_init(10);

mraa_pwm_context en1;
en1=mraa_pwm_init(6);
mraa_pwm_context en2;
en2=mraa_pwm_init(5);
mraa_gpio_dir(in1,MRAA_GPIO_OUT);
mraa_gpio_dir(in2,MRAA_GPIO_OUT);
mraa_pwm_enable(en1,1);
mraa_pwm_period_ms(en1,20);
mraa_gpio_dir(in3,MRAA_GPIO_OUT);
mraa_gpio_dir(in4,MRAA_GPIO_OUT);
mraa_pwm_enable(en2,1);
mraa_pwm_period_ms(en2,20);

mraa_pwm_write(en1,1.0f);
mraa_pwm_write(en2,1.0f);
mraa_gpio_write(in1,1);
mraa_gpio_write(in2,0);
mraa_gpio_write(in3,0);          
mraa_gpio_write(in4,1);          
printf("right90");
usleep(2300000);                 
mraa_gpio_write(in1,0);  
mraa_gpio_write(in2,0);  
mraa_gpio_write(in3,0);  
mraa_gpio_write(in4,0);  
mraa_pwm_enable(en1,0);
mraa_pwm_enable(en2,0);
usleep(2300000);
return 0;              
} 
Пример #3
0
void PWM_Run(void)
{
	mraa_pwm_period_ms(pwm, TIME_PERIOD);
	//printf("Time Period set to %dms\n", TIME_PERIOD);

	mraa_pwm_enable(pwm,1);
	//printf("PWM enabled\n");

	mraa_pwm_write(pwm, DUTY_CYCLE);
	//printf("Duty Cycle set to %f%%\n", (100 * DUTY_CYCLE));

	//printf("PWM running\n");
}
Пример #4
0
FskErr mraaPWMNew(FskPinPWM *pin, SInt32 number, const char *name)
{
	FskErr err;
	mraaPWM mpwm;

	err = FskMemPtrNewClear(sizeof(mraaPWMRecord), &mpwm);
	if (err) return err;

	mpwm->context = mraa_pwm_init(number);
	if (!mpwm->context) {
		FskMemPtrDispose(mpwm);
		return kFskErrOperationFailed;
	}

	mraa_pwm_period_ms(mpwm->context, PWMDEFAULTPERIOD);
	mpwm->period = PWMDEFAULTPERIOD;
	mraa_pwm_write(mpwm->context, 0.0);

	*pin = (FskPinPWM)mpwm;

	return err;
}
int main()
{
	int sockfd;
	float brightness;
	struct sockaddr_in server_addr;
	char response[MAX_SIZE];
	char cmd[MAX_SIZE];
	char cmd2[MAX_SIZE];                                
        FILE *fp, *fp2;                                          
        char component_string[MAX_SIZE];
	char value_string[MAX_SIZE]; 
	int value_int;

	mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm;
	speed_pwm_in1 = mraa_pwm_init(3);
	speed_pwm_in2 = mraa_pwm_init(5);
	turn_pwm = mraa_pwm_init(6);
	
	if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) {
		fprintf(stderr, "Failed to initialized.\n");
		return 1;
	}

	mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz
	mraa_pwm_enable(speed_pwm_in1, 1);
	mraa_pwm_period_us(speed_pwm_in2,870);
	mraa_pwm_enable(speed_pwm_in2, 1);
	
	mraa_pwm_period_ms(turn_pwm,20);
   	mraa_pwm_enable(turn_pwm, 1);

	mraa_pwm_write(speed_pwm_in1, 1.0f);
	mraa_pwm_write(speed_pwm_in2, 1.0f);
	mraa_pwm_write(turn_pwm, CENTER);

	//Set the protocol to mqtt and
	//restart iotkit-agent to ensure that it is running.
	system("iotkit-admin protocol mqtt");
	system("systemctl restart iotkit-agent");

	//create a socket with the following attributes:
	socklen_t len = sizeof(server_addr);	
	sockfd = socket(AF_INET, SOCK_DGRAM, 0);

	bzero(&server_addr,sizeof(server_addr));

	//Name of the socket as agreed with the server
	server_addr.sin_family = AF_INET;
	server_addr.sin_addr.s_addr = inet_addr(localhost);
	server_addr.sin_port = htons(41235);	
	
	if (bind(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
        	fprintf(stderr, "Failed to bind");
        	return 1;
    	}

	while(1)	
    	{
		//Use recvfrom function to send JSON message to iotkit-agent
		if(recvfrom(sockfd, response, MAX_SIZE, 0, (struct sockaddr *)&server_addr, &len) < 0)
    		{
			fprintf(stderr, "Failed to receive actuation\n");
    			return 1;
		}
		//printf("Received JSON message: %s\n", response);


		//---This part extracts the component name----------------
		snprintf(cmd, sizeof(cmd), "echo '%s' | sed 's/\"/ /g' | tr ' ' '\n' | awk '/component/{getline; getline; print}'", response);
		fp = popen(cmd, "r");

	  	if (fp == NULL) {
    			fprintf(stderr, "Failed to run command\n" );
    			exit(1);
  		}
	
	  	while (fgets(component_string, sizeof(component_string)-1, fp) != NULL) {
  		}
  		pclose(fp);

		//---This part extracts the value-------------------------		
		snprintf(cmd2, sizeof(cmd2), "echo '%s' | sed 's/\"/ /g' | tr ' ' '\n' | awk '/value/{getline; getline; print}'", response);

                fp2 = popen(cmd2, "r");

                if (fp2 == NULL) {
                        fprintf(stderr, "Failed to run command\n" );
                        exit(1);
                }

                while (fgets(value_string, sizeof(value_string)-1, fp) != NULL) {
                }
                pclose(fp2);

		//printf(value_string);
		//---This part is for actuation
		if (!strcmp(component_string, "vehicleControl\n")) {
			if (!strcmp(value_string, "forward\n")) {
				printf("Going forward for 1 second.\n\n");
				mraa_pwm_write(turn_pwm, CENTER); //straight                                                          
                                usleep(100000);                                                                                       
                                speed_control(speed_pwm_in1, speed_pwm_in2, 100);                                                     
                                sleep(1);                                                                                             
                                speed_control(speed_pwm_in1, speed_pwm_in2, 0);                                                       
			}
			else if (!strcmp(value_string, "reverse\n")) {
				printf("Going backward for 1 second.\n\n");
				mraa_pwm_write(turn_pwm, CENTER); //reverse                                                             
                                usleep(100000);                                                                                       
                                speed_control(speed_pwm_in1, speed_pwm_in2, -100);                                                     
                                sleep(1);                                                                                             
                                speed_control(speed_pwm_in1, speed_pwm_in2, 0); 
			}
			else if (!strcmp(value_string, "left\n")) {                                                                         
				printf("Making a left turn.\n\n");
				mraa_pwm_write(turn_pwm, CENTER - 0.015f); //left                                                              
                                usleep(100000);                                                                                       
                                speed_control(speed_pwm_in1, speed_pwm_in2, 100);                                                     
                                sleep(1);                                                                                             
                                speed_control(speed_pwm_in1, speed_pwm_in2, 0);                                                                                                                                               
                        }
			else if (!strcmp(value_string, "right\n")) {                                                                         
				printf("Making a right turn.\n\n");
				mraa_pwm_write(turn_pwm, CENTER + 0.015f); //right                                                           
                                usleep(100000);                                                                                       
                                speed_control(speed_pwm_in1, speed_pwm_in2, 100);                                                    
                                sleep(1);                                                                                             
                                speed_control(speed_pwm_in1, speed_pwm_in2, 0);                                                                                                                                               
                        }
			else
				fprintf(stderr, "Wrong actuation command received!\n");
		}		
	}
	close(sockfd);
	return 0;
} 
Пример #6
0
Файл: pwm.c Проект: Hbrinj/mraa
mraa_result_t
mraa_pwm_period(mraa_pwm_context dev, float seconds)
{
    return mraa_pwm_period_ms(dev, seconds * 1000);
}
int main(){
 








/**********************************************************************/
/**********************************************************************/
/* Begin. */
	mraa_uart_context uart;
	uart = mraa_uart_init(0);
	mraa_uart_set_baudrate(uart, 115200);
	char buffer[] = "hhh";
	char flush[]="xxxxxxxxxxxx";
	char str[] = "HELLO";
	if (uart == NULL) {
        	fprintf(stderr, "UART failed to setup\n");
        	printf("UART failed");
		return 1;
    	}
	printf("firstavail:%d\n",mraa_uart_data_available(uart,0));
	while (mraa_uart_data_available(uart,0))
	{
		mraa_uart_read(uart, flush, sizeof(uart));
		printf("Flush: %c %c %c %c %c %c %c %c",flush[0], flush[1], flush[2],flush[3],flush[4],flush[5],flush[6],flush[7]);
		usleep(150);
	}
	printf("available: %d",mraa_uart_data_available(uart,0));
        char speed_user_input[MAXBUFSIZ];
        char turn_user_input[MAXBUFSIZ];
        mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm;
        speed_pwm_in1 = mraa_pwm_init(3);
        speed_pwm_in2 = mraa_pwm_init(5);
        turn_pwm = mraa_pwm_init(6);
        if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) {
                fprintf(stderr, "Failed to initialized.\n");
                return 1;
        }

        mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz
        mraa_pwm_enable(speed_pwm_in1, 1);
        mraa_pwm_period_us(speed_pwm_in2,870);
        mraa_pwm_enable(speed_pwm_in2, 1);

        mraa_pwm_period_ms(turn_pwm,20);
        mraa_pwm_enable(turn_pwm, 1);

        mraa_pwm_write(turn_pwm, CENTER);
        mraa_pwm_write(speed_pwm_in1, 1.0f);
        mraa_pwm_write(speed_pwm_in2, 1.0f);

        int n = 5;
        int spd = 0;
        char direction = 'C';
	char cross_clr = 'B';
        float angle = 0.0f;
	while (1)
        {
	//sleep(2);
        //readCharAry(uart,flush);
	spd = 50;
	mraa_uart_read(uart, buffer, 1);
	if(buffer[0]=='\n')
	{
		printf("new line ");	
		mraa_uart_read(uart, buffer, 1);
		if(buffer[0]=='\n')
		{
			printf("new line ");
                	mraa_uart_read(uart, buffer, 1);
		}
	}
	mraa_uart_read(uart, buffer+1, 1);
	if(buffer[1]=='\n')
        {
		buffer[0] = 'h';
		buffer[1] = 'h';
        }
	int sign = 0;
	if(cross_clr == 'M')	
	{
		speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f);
		sleep(1);
		char* nearestBeaconID = (char*)malloc(5);
		getStrongestBeacon(nearestBeaconID);
		printf("the nearestBeaconID is: %s\n", nearestBeaconID);
		sign = 1;
		angle = CENTER + 0.015f;
        mraa_pwm_write(turn_pwm, angle);
        usleep(15000);
        speed_control(speed_pwm_in1, speed_pwm_in2, 54);
        printf("speed: %d",spd);
        usleep(5500000);
                speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f);
		//sleep(1);
		mraa_uart_write(uart, str, 1);
		//while(!mraa_uart_data_available(uart, 10)){};
        mraa_uart_read(uart, buffer, 1);
        while(buffer[0] == '\n')
		{
			mraa_uart_read(uart, buffer, 1);
		}
		if(buffer[0] == '\0') direction = 'C';
                mraa_uart_read(uart, buffer + 1, 1);
                cross_clr = buffer[1];
	}
	printf("buff:%c %c %c \n",buffer[0], buffer[1],buffer[2]);
	if(!sign){
        if (direction == 'L')   angle = CENTER - 0.005f;
        if (direction == 'R')   angle = CENTER + 0.005f;
        if (direction == 'C')   angle = CENTER;}
	else
	{
		if (direction == 'C')	angle = CENTER +0.013f; 
		if (direction == 'L')   angle = CENTER +0.005f;
        	if (direction == 'R')   angle = CENTER + 0.019f;
	}
	mraa_pwm_write(turn_pwm, angle);
        speed_control(speed_pwm_in1, speed_pwm_in2, spd);
	printf("speed: %d",spd);
	usleep(250000);

	direction = buffer[0];
	cross_clr = buffer[1];
        }
        return 0;
}
int main() {
    float speed, turn;
    char speed_user_input[MAXBUFSIZ];
    char turn_user_input[MAXBUFSIZ];
    char time_user_input[MAXBUFSIZ];
    mraa_pwm_context speed_pwm_in1, speed_pwm_in2, turn_pwm;
    speed_pwm_in1 = mraa_pwm_init(3);
    speed_pwm_in2 = mraa_pwm_init(5);
    turn_pwm = mraa_pwm_init(6);

    if (speed_pwm_in1 == NULL || speed_pwm_in2 == NULL || turn_pwm == NULL) {
        fprintf(stderr, "Failed to initialized.\n");
        return 1;
    }

    mraa_pwm_period_us(speed_pwm_in1,870); //1150Hz
    mraa_pwm_enable(speed_pwm_in1, 1);
    mraa_pwm_period_us(speed_pwm_in2,870);
    mraa_pwm_enable(speed_pwm_in2, 1);

    mraa_pwm_period_ms(turn_pwm,20);
    mraa_pwm_enable(turn_pwm, 1);

    mraa_pwm_write(turn_pwm, CENTER);
    mraa_pwm_write(speed_pwm_in1, 1.0f);
    mraa_pwm_write(speed_pwm_in2, 1.0f);

    RearWheel *rw = new RearWheel();

    //go for the curve
    thread t1;

    while(1) {
        t1 = rw->threading();
        printf("Turn (L, C, R): ");
        scanf("%s", turn_user_input);

        if (!strcmp(turn_user_input, "L") || !strcmp(turn_user_input, "l"))
            turn = CENTER - 0.015f;
        else if (!strcmp(turn_user_input, "C") || !strcmp(turn_user_input, "c"))
            turn = CENTER;
        else if (!strcmp(turn_user_input, "R") || !strcmp(turn_user_input, "r"))
            turn = CENTER + 0.018f;
        else {
            printf("Wrong turn type!\n");
            return 1;
        }

        printf("Speed value (-100-100): ");
        scanf("%s", speed_user_input);
        speed = atof(speed_user_input);

        printf("time: ");
        scanf("%s", time_user_input);
        int time = atoi(time_user_input);

        if (speed > 100 || speed < -100)
            printf("Error: Choose between -100 and 100\n");
        else {
            mraa_pwm_write(turn_pwm, turn);
            usleep(100000);
            speed_control(speed_pwm_in1, speed_pwm_in2, speed);
        }
        sleep(time);
        speed_control(speed_pwm_in1, speed_pwm_in2, 0.0f);
        float count = rw->getCount();
        printf("%.2f \n", count);
        rw->clear();
        t1.join();
    }
    return 0;
}
Пример #9
0
int main(int argc, char *argv[])

{
in1=mraa_gpio_init(7); 
in2=mraa_gpio_init(8); 
in3=mraa_gpio_init(9); 
in4=mraa_gpio_init(10);


en1=mraa_pwm_init(6);                                                
en2=mraa_pwm_init(5);                                                
mraa_gpio_dir(in1,MRAA_GPIO_OUT);                                    
mraa_gpio_dir(in2,MRAA_GPIO_OUT);                                    
mraa_pwm_period_ms(en1,20);                                          
mraa_pwm_enable(en1,1);                                              
mraa_gpio_dir(in3,MRAA_GPIO_OUT);                                    
mraa_gpio_dir(in4,MRAA_GPIO_OUT);           
mraa_pwm_period_ms(en2,20);                 
mraa_pwm_enable(en2,1); 

char* ptr1 = argv[1];
char* ptr2 = argv[2]; 
char* ptr3 = argv[3]; 

float param,result;
int i= atoi(ptr1);
int j=atoi(ptr2);
int k= atoi(ptr3);

if (i == -2)
{
rotate_right(2);//because 20 degrees
}
else if( i == -1)
{
rotate_right90(); //calcualte for 90 degrees
move_front(29);   // 20 cms
rotate_left90();  //calculate for 90 degrees
}

else {


param=i/j;
//convert to our metrix, in terms of distance moved by robot//

result = atan((param) * 180 / PI);

if(result < 0)

{
rotate_left((int)(result/10));
}

else
{
rotate_right((int)(result/10));
}

float dist=sqrt(((i*i)+(j*j)));
if(k == 0)
{
dist = dist/3;
}

int num = (int) dist/(0.7) ;

move_front(num);
}                               
return 0;                        
} 
Пример #10
0
 /**
  * Set period, milliseconds
  *
  * @param ms milliseconds for period
  * @return Result of operation
  */
 mraa_result_t
 period_ms(int ms)
 {
     return mraa_pwm_period_ms(m_pwm, ms);
 }