Exemple #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;
}
Exemple #2
0
es9257_context es9257_init(int32_t pin, int32_t min_pulse_width,
                         int32_t max_pulse_width) {

    // make sure MRAA is initialized
    int mraa_rv;
    if ((mraa_rv = mraa_init()) != MRAA_SUCCESS)
    {
        printf("%s: mraa_init() failed (%d).\n", __FUNCTION__, mraa_rv);
        return NULL;
    }

    es9257_context dev = (es9257_context) malloc(sizeof(struct _es9257_context));

    if(dev == NULL){
        printf("Unable to assign memory to the Servo motor structure");
        return NULL;
    }

    dev->servo_pin = pin;

    // second is the min pulse width
    dev->min_pulse_width = min_pulse_width;
    // third is the max pulse width
    dev->max_pulse_width = max_pulse_width;

    dev->pwm = mraa_pwm_init(dev->servo_pin);
    if(dev->pwm == NULL){
        printf("Unable to initialize the PWM pin");
    }

    es9257_set_angle(dev, 0);
    return dev;
}
Exemple #3
0
mraa_pwm_context setupPWMO( int pinNumber ) {

    on = 0;

    mraa_pwm_context pwmo = mraa_pwm_init( pinNumber );
    if ( ! pwmo ) {
        printf( "  Failed initing pwmo\n" );
        mraa_result_print( MRAA_ERROR_UNSPECIFIED );
    	return 0;
    }

    response = mraa_pwm_enable( pwmo, 0 );					// Initially off
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo enable to off during setup\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }
    response = mraa_pwm_config_percent( pwmo, 100, 0.25 );	// Startup settings, 0.1 second, 50% duty cycle
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo period and duty cycle\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }
    response = mraa_pwm_enable( pwmo, 1 );					// Now enable it
    if (response != MRAA_SUCCESS) {
//        printf( "  Failed setting pwmo enable to on during setup\n" );
        mraa_result_print((mraa_result_t) response);
        return 0;
    }

//    printf( "  Inited pwmo for pin %d\n", pinNumber );
   	return pwmo;
}
tb6612::tb6612():
  _dcA(0.0), _dcB(0.0)
{
  
  // _pwmA is pwm channel 0, on pin 20 in mraa
  _pwmA = mraa_pwm_init(20);
  mraa_pwm_period_us(_pwmA, 1000);
  mraa_pwm_enable(_pwmA, 1);
 
  // _pwmB is pwm channel 1, on pin 14 in mraa
  _pwmB = mraa_pwm_init(14);
  mraa_pwm_period_us(_pwmB, 1000);
  mraa_pwm_enable(_pwmB, 1);

  mraa_pwm_write(_pwmA, 0.01);
  mraa_pwm_write(_pwmB, 0.01);
  mraa_pwm_write(_pwmA, 0.0);
  mraa_pwm_write(_pwmB, 0.0);

  // _A1 and _A2 are on GPIO48 and GPIO47, respectively, which are pins 33 and
  //  46 in mraa, respectively.
  _A1 = mraa_gpio_init(33);
  _A2 = mraa_gpio_init(46);
  mraa_gpio_dir(_A1, MRAA_GPIO_OUT);
  mraa_gpio_mode(_A1, MRAA_GPIO_STRONG);
  mraa_gpio_write(_A1, 1);
  mraa_gpio_dir(_A2, MRAA_GPIO_OUT);
  mraa_gpio_mode(_A2, MRAA_GPIO_STRONG);
  mraa_gpio_write(_A2, 1);

  // _B1 and _B2 are on GPIO15 and GPIO14, respectively, which are pins 48 and
  //  36, respectively
  _B1 = mraa_gpio_init(48);
  _B2 = mraa_gpio_init(36);
  mraa_gpio_dir(_B1, MRAA_GPIO_OUT);
  mraa_gpio_mode(_B1, MRAA_GPIO_STRONG);
  mraa_gpio_write(_B1, 1);
  mraa_gpio_dir(_B2, MRAA_GPIO_OUT);
  mraa_gpio_mode(_B2, MRAA_GPIO_STRONG);
  mraa_gpio_write(_B2, 1);

  // _standbyPin is on GPIO49, which is pin 47 in mraa
  _standbyPin = mraa_gpio_init(47);
  mraa_gpio_dir(_standbyPin, MRAA_GPIO_OUT);
  mraa_gpio_mode(_standbyPin, MRAA_GPIO_STRONG);
  mraa_gpio_write(_standbyPin, 1);
}
Exemple #5
0
void init(int pin1, int pin2, int minPulseWidth, int maxPulseWidth,
		int waitAndDisablePwm) {
	m_minPulseWidth = minPulseWidth;
	m_maxPulseWidth = maxPulseWidth;
	m_period = PERIOD;

	m_waitAndDisablePwm = waitAndDisablePwm;

	m_maxAngle = 180.0;

	m_pwmServoContext = mraa_pwm_init(pin1);
	m_pwmServoContext1 = mraa_pwm_init(pin2);

	m_currAngle = 180;

//	setAngle(0, pin3);
//	setAngle(0, pin9);
}
Exemple #6
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;              
} 
Exemple #7
0
void PWM_Init(void)
{
	pwm = mraa_pwm_init(20); //J18-7 --> GPIO-12 --> PWM0

	if(pwm == NULL)
	{
		printf("PWM initialization failed, check syslog for details, exit...\n");
		exit(1);
	}

	//printf("PWM initialized successfully\n");
}
Exemple #8
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;
	}
void initialize()
{
	pwm1 = mraa_pwm_init(20);
	mraa_pwm_period_us(pwm1, 870);
	mraa_pwm_enable(pwm1, 1);

	pwm2 = mraa_pwm_init(14);
	mraa_pwm_period_us(pwm2, 870);
	mraa_pwm_enable(pwm2, 1);

	mraa_pwm_write(pwm1, 0.01);
	mraa_pwm_write(pwm2, 0.01);
	mraa_pwm_write(pwm1, 0.0);
	mraa_pwm_write(pwm2, 0.0);

	A1 = mraa_gpio_init(48);
	A2 = mraa_gpio_init(36);
	mraa_gpio_dir(A1, MRAA_GPIO_OUT);
	mraa_gpio_mode(A1, MRAA_GPIO_STRONG);
	mraa_gpio_write(A1, 1);
	mraa_gpio_dir(A2, MRAA_GPIO_OUT);
	mraa_gpio_mode(A2, MRAA_GPIO_STRONG);
	mraa_gpio_write(A2, 1);

	B1 = mraa_gpio_init(33);
	B2 = mraa_gpio_init(46);
	mraa_gpio_dir(B1, MRAA_GPIO_OUT);
	mraa_gpio_mode(B1, MRAA_GPIO_STRONG);
	mraa_gpio_write(B1, 1);
	mraa_gpio_dir(B2, MRAA_GPIO_OUT);
	mraa_gpio_mode(B2, MRAA_GPIO_STRONG);
	mraa_gpio_write(B2, 1);

	standbyPin = mraa_gpio_init(47);
	mraa_gpio_dir(standbyPin, MRAA_GPIO_OUT);
	mraa_gpio_mode(standbyPin, MRAA_GPIO_STRONG);
	mraa_gpio_write(standbyPin, 1);
}
void
VexMotorController::init(int pin, int fullReversePulseWidth, int neutralPulseWidth, int fullForwardPulseWidth)
{
	m_fullReversePulseWidth 	= fullReversePulseWidth;
	m_neutralPulseWidth 		= neutralPulseWidth;
	m_fullForwardPulseWidth 	= fullForwardPulseWidth;
	m_period 					= PERIOD;

	m_servoPin					= pin;

	m_pwmServoContext 			= mraa_pwm_init(m_servoPin);

	setSpeed(0);
}
Exemple #11
0
    /**
     * instanciates a PWM object on a pin
     *
     * @param pin the pin number used on your board
     * @param owner if you are the owner of the pin the destructor will
     * @param chipid the pwmchip to use, use only in raw mode
     * unexport the pin from sysfs, default behaviour is you are the owner
     * if the pinmapper exported it
     */
    Pwm(int pin, bool owner = true, int chipid = -1)
    {
        if (chipid == -1) {
            m_pwm = mraa_pwm_init(pin);
        } else {
            m_pwm = mraa_pwm_init_raw(chipid, pin);
        }

        if (m_pwm == NULL) {
            throw std::invalid_argument("Error initialising PWM on pin");
        }

        if (!owner) {
            mraa_pwm_owner(m_pwm, 0);
        }
    }
Exemple #12
0
// initialization in PWM mode
speaker_context speaker_init_pwm(int pin)
{
    speaker_context dev = NULL;
    if (!(dev = _common_init()))
        return NULL;

    dev->is_pwm = true;

    if (!(dev->pwm = mraa_pwm_init(pin)))
    {
        printf("%s: mraa_pwm_init() failed.\n", __FUNCTION__);
        speaker_close(dev);
        return NULL;
    }

    // set the default frequency to 1Khz
    dev->default_freq = 1000;

    return dev;
}
Exemple #13
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;
}
Exemple #14
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;                        
} 
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;
}
BuzzerAlarm::BuzzerAlarm(int pin) {
	myAlarm = mraa_pwm_init(pin);
	mraa_pwm_enable(myAlarm, ENABLE);
}
int main(int argc, char *argv[]) {
	
	printf("\n---------------------ir_transmit.c---------------------\n");
	//const struct sched_param priority={1};
	//sched_setscheduler(0,SCHED_FIFO,&priority);
	
	int transmit_counter = 0;
	int i = 0;
	int temp = -1;
	// GPIO Initialization - Edison has 4 PWM pins
	
	pwm1 = mraa_pwm_init(3);
	mraa_pwm_period_us(pwm1, 26);
	mraa_pwm_enable(pwm1, 1);
	
	pwm2 = mraa_pwm_init(5);
	mraa_pwm_period_us(pwm2, 26);
	mraa_pwm_enable(pwm2, 1);
	
	pwm3 = mraa_pwm_init(6);
	mraa_pwm_period_us(pwm3, 26);
	mraa_pwm_enable(pwm3, 1);
	
	pwm4 = mraa_pwm_init(9);
	mraa_pwm_period_us(pwm4, 26);
	mraa_pwm_enable(pwm4, 1);

	printf("\n---------------------Part 0: Apply Settings from Config Files---------------------\n");
	temp = check_preamble_length();
	if (temp != -1) {
		printf("\nGot Preamble Length from /etc/IR/preamble_length.txt\n");
		printf("ID I got was %d\n", temp);
		printf("But we are still going to use 5\n", temp);
		//preamble_length = temp;
	}
	temp = -1;
	
	// check file for edisonID
	temp = check_edison_id();
	if (temp != -1) {
		printf("Got Edison ID from /etc/IR/edisonID.txt\n");
		printf("ID I got was %d\n", temp);
		edisonID = temp;
	}
	temp = -1;
	
	printf("\n---------------------Part 0: Apply Settings from Passed in Arguments---------------------\n");
	// if we pass in an argument, use it for the preamble_length, takes
	// priority over what's in the file. please pass in an integer
	if (argc == 2) {
		preamble_length = atoi(argv[1]);
		printf("Argument(1) Passed In! Preamble Length set to: %d\n", preamble_length);
	}
	else {
		printf("No Argument(1), preamble Length set to default value: %d\n", preamble_length);
	}
	// if we pass in an argument, use it for the edisonID, takes
	// priority over what's in the file. please pass in an integer
	if (argc == 3) {
		preamble_length = atoi(argv[1]);
		printf("Argument(1) Passed In! Preamble Length set to: %d\n", preamble_length);
		
		edisonID = atoi(argv[2]);
		printf("Argument(2) Passed In! edisonID set to: %d\n", edisonID);		
	}
	else {
		printf("No Argument(2), edisonID set to default value: %d\n", edisonID);		
	}	
	// This will transmit IR data on all 4 pins at once
	// on a single Edison board

	
	// Continuously Transmit IR Signal when the program is running
	while(1) {
		printf("\n---------------------Transmission---------------------\n");
	
		// Updates the preamble_length variable based on info from server
		temp = check_preamble_length();
		if (temp != -1) {
			printf("ID from /etc/IR_conf/preamble_length.txt was %d\n", temp);
			preamble_length = temp;
		}
		
		// Preamble - Signals the Receiver Message Incoming
		printf("Sending preamble_length = %d, ", preamble_length);
		send_preamble_sequence(preamble_length);
		
		// Sending Edison Board ID # - 2 bits, MSB then LSB
		printf("EdisonID: %d - ", edisonID);
		switch (edisonID) {
			case 0:
				send_low_bit();		// Send lsb bit 0 = LOW
				send_low_bit();		// Send msb bit 1 = LOW
				break;
			case 1:
				send_low_bit();		// Send msb bit 1 = LOW
				send_high_bit();	// Send lsb bit 0 = HIGH
				break;
			case 2:
				send_high_bit();	// Send msb bit 1 = HIGH
				send_low_bit();		// Send lsb bit 0 = LOW
				break;
			case 3:
				send_high_bit();	// Send lsb bit 0 = HIGH
				send_high_bit();	// Send msb bit 1 = HIGH
				break;
			default:
				send_low_bit();		// Send lsb bit 0 = LOW
				send_low_bit();		// Send msb bit 1 = LOW				
		}
		
		// Sending Edison IR Emitter ID # - 2 bits, MSB then LSB
		
		// pwm1,DUTY = 00 = short-long/short-long = 5-20/5-20
		// pwm2,DUTY = 01 = short-long/long-short = 5-20/20-5
		// pwm3,DUTY = 10 = long-short/short-long = 20-5/5-20
		// pwm4,DUTY = 11 = long-short/long-short = 20-5/20-5
		
		printf(", EmitterIDs 0-3\n");
		// First Bit
		mraa_pwm_write(pwm1,DUTY);
		mraa_pwm_write(pwm2,DUTY);
		mraa_pwm_write(pwm3,DUTY);
		mraa_pwm_write(pwm4,DUTY);
		for(i = SHORT_DELAY; i > 0; i--); // 5 ms
		
		mraa_pwm_write(pwm1,0);
		mraa_pwm_write(pwm2,0);
		//mraa_pwm_write(pwm3,DUTY, DUTY);
		//mraa_pwm_write(pwm4,DUTY, DUTY);
		for(i = MID_DELAY; i > 0; i--); // 15 ms

		//mraa_pwm_write(pwm1, 0);
		//mraa_pwm_write(pwm2, 0);
		mraa_pwm_write(pwm3,0);
		mraa_pwm_write(pwm4,0);
		for(i = SHORT_DELAY; i > 0; i--); // 5 ms
		
		// Second Bit	
		mraa_pwm_write(pwm1,DUTY);
		mraa_pwm_write(pwm2,DUTY);
		mraa_pwm_write(pwm3,DUTY);
		mraa_pwm_write(pwm4,DUTY);
		for(i = SHORT_DELAY; i > 0; i--); // 20 ms

		mraa_pwm_write(pwm1,0);
		//mraa_pwm_write(pwm2,DUTY, DUTY);	
		mraa_pwm_write(pwm3,0);
		//mraa_pwm_write(pwm4,DUTY, DUTY);	
		for(i = MID_DELAY; i > 0; i--); // 15 ms

		//mraa_pwm_write(pwm1, 0);
		mraa_pwm_write(pwm2,0);
		//mraa_pwm_write(pwm3, 0);
		mraa_pwm_write(pwm4,0);
		for(i = SHORT_DELAY; i > 0; i--); // 5 ms
		
		/*
		send_message_bit(edisonID, 0, DUTY, pwm1,DUTY);
		send_message_bit(edisonID, 0, DUTY, pwm1,DUTY);
		send_message_bit(edisonID, 0, DUTY, pwm1,DUTY);
		send_message_bit(edisonID, 0, DUTY, pwm1,DUTY);
		
		
		for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0; transmit_counter--) {
			send_message_bit(edisonID, 0, DUTY, pwm1,DUTY);
		}
		for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0; transmit_counter--) {
			send_message_bit(edisonID, 1, DUTY, pwm2,DUTY);			
		}
		for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0; transmit_counter--) {
			send_message_bit(edisonID, 2, DUTY, pwm3,DUTY);
		}
		for (transmit_counter = TRANSMIT_DURATION; transmit_counter >= 0 ;transmit_counter--) {
			send_message_bit(edisonID, 3, DUTY, pwm4,DUTY);
		}
		*/
		
	}// end while loop

	mraa_pwm_write (pwm1, 0);
	mraa_pwm_enable(pwm1, 0);
	mraa_pwm_write (pwm2, 0);
	mraa_pwm_enable(pwm2, 0);
	mraa_pwm_write (pwm3, 0);
	mraa_pwm_enable(pwm3, 0);
	mraa_pwm_write (pwm4, 0);
	mraa_pwm_enable(pwm4, 0);

	return 0;
}//end main
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;
} 
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;
}