Ejemplo n.º 1
0
int serial_avail(void)
{
  int k =-1;
  k = mraa_uart_data_available(uart, 0);
  return k;

}
Ejemplo n.º 2
0
mrb_value mrb_mraa_uart_read_to_prompt(mrb_state *mrb, mrb_value self) {
  mraa_uart_context uart;
  mrb_int timeout, nargs, i;
  char prompt, str_arg[1], buf[1];
  mrb_value result;  
  
  timeout = mrb_fixnum(IV_GET("@timeout"));
  
  nargs = mrb_get_args(mrb, "|z", &str_arg);
  if (nargs == 0) {
    char *str = mrb_str_to_cstr(mrb, IV_GET("@prompt"));
    prompt = str[0];
  }
  else
    prompt = str_arg[0];
  
  uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type);
  result = mrb_str_buf_new(mrb, mrb_fixnum(IV_GET("@read_bufsize")));
  
  while (mraa_uart_data_available(uart, timeout) > 0) {
    i = mraa_uart_read(uart, buf, 1);
    if (i == 0)         break;
    if (*buf == prompt) break;
    result = mrb_str_cat_cstr(mrb, result, buf);
  }

  return result;
}
Ejemplo n.º 3
0
Archivo: uart.hpp Proyecto: emutex/mraa
 /**
  * Check to see if data is available on the device for reading
  *
  * @param millis number of milliseconds to wait, or 0 to return immediately
  * @return true if there is data available to read, false otherwise
  */
 bool
 dataAvailable(unsigned int millis = 0)
 {
     if (mraa_uart_data_available(m_uart, millis))
         return true;
     else
         return false;
 }
Ejemplo n.º 4
0
bool rn2903_data_available(const rn2903_context dev, unsigned int millis)
{
    assert(dev != NULL);

    if (mraa_uart_data_available(dev->uart, millis))
        return true;
    else
        return false;
}
Ejemplo n.º 5
0
int is_incoming_modem(){
	char msg[40];
	int x=0;
	if ( (x = mraa_uart_data_available(uart,0) ) < 0   ) {
	  		writeToFile(src, "error reading data from uart\n");
  			exit(UART_ERROR);
	}	
	return x;
}
Ejemplo n.º 6
0
mrb_value mrb_mraa_uart_data_available(mrb_state *mrb, mrb_value self) {
  mraa_uart_context uart;
  unsigned int millis, nargs;
  nargs = mrb_get_args(mrb, "|i", &millis);
  if (nargs == 0)
    millis = 0;

  uart = (mraa_uart_context)mrb_data_get_ptr(mrb, self, &mrb_mraa_uart_ctx_type);
  return ( mraa_uart_data_available(uart, millis) == 1 ) ? mrb_true_value() : mrb_false_value();
}
Ejemplo n.º 7
0
void Modem_SIM800::WriteLine(std::string line)
{
    while(mraa_uart_data_available(this->_serial, 100))
    {
        char deadBuf[2] = {0};
        mraa_uart_read(this->_serial, deadBuf, 1);
    } // clear responses
    mraa_uart_write(this->_serial, line.c_str(), strlen(line.c_str()));
    mraa_uart_write(this->_serial, "\r\n", strlen("\r\n"));
    Logging::LogMessage(std::string("--> ") + line);
}
Ejemplo n.º 8
0
int is_incoming_modem(){
	char msg[40];
	int x=0;
	if ( (x = mraa_uart_data_available(uart,0) ) < 0   ) {
	  		writeToFile(src, "error reading data from uart\n");
  			exit(UART_ERROR);
	}	
	/*else if(x>0)
		printf("at is_incoming_modem X (m_len outside the function) is:%d\n", x);*/
	return x;
}
Ejemplo n.º 9
0
void Modem_SIM800::Init()
{
    _enabled = true;
    this->_serial = mraa_uart_init(0);
    mraa_uart_set_baudrate(this->_serial, 9600);

    // TODO: Error handling
    while(mraa_uart_data_available(this->_serial, 0)) {this->Read();} // clear anything the modem sent on startup

    // get the auto-bauder started
    this->WriteLine("ATE0"); this->Read(); // disable echo of commands

    if (mraa_uart_data_available(this->_serial, 100))
    {
         this->Read(); // read extra echo
    }

    this->WriteLine("AT"); this->Read();

    this->WriteLine("AT"); this->Read();

    this->WriteLine("AT"); this->Read();


    while(mraa_uart_data_available(this->_serial, 100))
    {
        char deadBuf[2] = {0};
        mraa_uart_read(this->_serial, deadBuf, 1);
    } // clear responses

    if(!this->CheckReply("ATE0", "OK"))
    {
        // TODO: error handle
    }

    Logging::LogMessage(this->GetOperator());
}
Ejemplo n.º 10
0
Archivo: ecezo.c Proyecto: g-vidal/upm
static bool ecezo_data_available(const ecezo_context dev, unsigned int millis)
{
    assert(dev != NULL);

    // i2c, we don't support this
    if (dev->i2c)
    {
        return false;
    }

    // uart
    if (mraa_uart_data_available(dev->uart, millis))
        return true;
    else
        return false;
}
Ejemplo n.º 11
0
/**
 * n_direction_flag: 0 from edison to beaglebone
 *                  1 from beaglebone to edison
 * check https://github.com/peidong/drone/blob/master/Edison/main/edison-bbb-communication-code.md for commands
 */
int communication_with_beaglebone_uart(int nflag_direction, struct T_drone *pT_drone, int nflag_receive_success){
    /**
     * check if uart available
     */
    while (pT_drone->nflag_enable_uart != 1){
        usleep(1300);
    }
    pT_drone->nflag_enable_uart = 0;
    mraa_uart_context beaglebone_uart;
    if (nflag_direction == 1){
        /**
         * From beaglebone to edison
         */
        beaglebone_uart = mraa_uart_init_raw("/dev/ttyO4");
        mraa_uart_set_baudrate(beaglebone_uart, 38400);
        mraa_uart_set_mode(beaglebone_uart, 8, MRAA_UART_PARITY_NONE , 1);
        while (mraa_uart_data_available(beaglebone_uart, 10000) != 1){
            printf("data not available\n");
            /*usleep(10000);*/
        }
        /**
         * Start receive
         */
        char c_flag[1];
        char arrc_buffer[20];
        int nflag_find_beginning = 0;
        int nflag_find_end = 0;
        int n_index = 0;
        /**
         * Read the message array
         */
        while (nflag_find_beginning != 1){
            mraa_uart_read(beaglebone_uart, c_flag, 1);
            if (c_flag[0] == '~'){
                nflag_find_beginning = 1;
                n_index = 0;
                while (nflag_find_end != 1){
                    mraa_uart_read(beaglebone_uart, arrc_buffer + n_index, 1);
                    if (arrc_buffer[n_index] == '$'){
                        arrc_buffer[n_index] = '\0';
                        nflag_find_end = 1;
                        //break;
                    }else if (arrc_buffer[n_index] == '~'){
                        nflag_find_end = -1;
                        nflag_find_beginning = 1;
                        n_index = 0;
                        //continue;
                    }else{
                        n_index++;
                    }
                }
            }
        }
         /**
         * Process the message
         */
        int n_command_index = -1;
        if (arrc_buffer[0] == '0'){
            /**
             * stop
             */
            n_command_index = 0;
            printf("stop received\n");
        }else if (arrc_buffer[0] == '1'){
            /**
             * auto control
             */
            char arrc_command_index[4];
            int n_temp_index;
            for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){
                arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index];
            }
            arrc_command_index[3] = '\0';
            n_command_index = atoi(arrc_command_index);
            printf("auto control received: %d\n", n_command_index);
        }else if (arrc_buffer[0] == '2'){
            /**
             * manual control command
             */
            char arrc_command_index[4];
            int n_temp_index;
            for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){
                arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index];
            }
            arrc_command_index[3] = '\0';
            n_command_index = atoi(arrc_command_index);
            printf("manual control received: %d\n", n_command_index);
        }else if (arrc_buffer[0] == '3'){
            /**
             * pid tuning
             */
            char arrc_command_index[4];
            int n_temp_index;
            for (n_temp_index = 0; n_temp_index <= 2; n_temp_index++){
                arrc_command_index[n_temp_index] = arrc_buffer[n_temp_index];
            }
            arrc_command_index[3] = '\0';
            n_command_index = atoi(arrc_command_index);
            printf("pid tuning received: %d\n", n_command_index);

            char arrc_pid_value[9];
            for (n_temp_index = 0; n_temp_index <= 7; n_temp_index++){
                arrc_pid_value[n_temp_index] = arrc_buffer[n_temp_index + 3];
            }
            arrc_buffer[8] = '\0';
            if (n_command_index == 301){
                pT_drone->d_kp_pitch = atof(arrc_pid_value);
            }else if (n_command_index == 302){
                pT_drone->d_ki_pitch = atof(arrc_pid_value);
            }else if (n_command_index == 303){
                pT_drone->d_kd_pitch = atof(arrc_pid_value);
            }else if (n_command_index == 304){
                pT_drone->d_kp_roll = atof(arrc_pid_value);
            }else if (n_command_index == 305){
                pT_drone->d_ki_roll = atof(arrc_pid_value);
            }else if (n_command_index == 306){
                pT_drone->d_kd_roll = atof(arrc_pid_value);
            }else if (n_command_index == 307){
                pT_drone->d_kp_yaw = atof(arrc_pid_value);
            }else if (n_command_index == 308){
                pT_drone->d_kd_yaw = atof(arrc_pid_value);
            }else if (n_command_index == 309){
                pT_drone->d_ki_yaw = atof(arrc_pid_value);
            }else if (n_command_index == 310){
                pT_drone->d_kp_second_pitch = atof(arrc_pid_value);
            }else if (n_command_index == 311){
                pT_drone->d_kd_second_pitch = atof(arrc_pid_value);
            }else if (n_command_index == 312){
                pT_drone->d_kp_second_roll = atof(arrc_pid_value);
            }else if (n_command_index == 313){
                pT_drone->d_kd_second_roll = atof(arrc_pid_value);
            }else if (n_command_index == 314){
                pT_drone->d_kp_second_yaw = atof(arrc_pid_value);
            }else if (n_command_index == 315){
                pT_drone->d_kd_second_yaw = atof(arrc_pid_value);
            }
            printf("pid tuning value: %lf\n", atof(arrc_pid_value));
        }else if (arrc_buffer[0] == '4'){
            /**
             * feedback
             */
            printf("feedback received\n");
        }
    }else if (nflag_direction == 0){
        /**
         * From edison to beaglebone
         */
    }
    pT_drone->nflag_enable_uart = 1;
    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;
}