Example #1
0
int main(int argc, char* argv[])
{
	printf("[i] Start... \n");

	Serial serial;
#if defined(WIN32)
	serial.open(SERIAL_PORT_NAME, SERIAL_PORT_RATE, true);
#elif defined(LINUX)
	serial.open(SERIAL_PORT_NAME, SERIAL_PORT_RATE);
#endif

	if(!serial.connected())
	{
		printf("[!] Cant open port!\n");
		return -1;
	}

	char c = 'y';
	int res = 12;

	char buf[BUF_SIZE1];
	memset(buf, 0, BUF_SIZE1);

	while(true)
	{

		serial.write( &c, 1 );
#if 0
		if(res = serial.available()){
			if( res = serial.Read(buf, res) ){
				printf("%d %s\n", res, buf);
			}
		}
#else
		if(serial.waitInput(1000)==0)
			printf("[i] timeout!\n");
		else
		{
			if(res = serial.available())
			{
				res = serial.read(buf, res);
				printf("%d %s\n", res, buf);
			}
		}
#endif
	}
	serial.close();

	printf("[i] End.\n");
	return 0;
}
TEST(Serial, WriteCOM) {

	Serial serial;

	serial.open("COM7", 9600);

	if(!serial.connected()){
		FAIL()<<"cant open serial port!";
	}

	char buf[1024];
	int res = 0;
	while(1){

		char wbuf[] = {0xff, 0xff, 0x00, 0x00, 0x00, 0x00 ,0xff};

		serial.write(wbuf, sizeof(wbuf));

		if( serial.waitInput(1000) > 0){
			if( (res = serial.available()) > 0 ){
				if( res = serial.read(buf, res) ){
					printf("[i] read data(%d): \n", res);
					for(int i=0; i<res; i++){
						printf("%02X ", (unsigned char)buf[i]);
						if(i>0 && (i+1)%16 == 0) {
							printf("\t");
							for(int j=i-15; j<=i; j++){
								printf("%c", buf[j]);
							}
							printf("\n");
						}
					}
					printf("\n");
					res = 0;
				}
			}
		}
		Sleep(1000);
	}
}
Example #3
0
void process_serial(Serial &serial, TBuff<uint8_t> &buff, orcp2::packet &pkt)
{
    int res = 0;

    if(!buff.data || !pkt.message.data) {
        ROS_ERROR("[!] Error: no memory!\n");
        return ;
    }

    ros::Time current_time;

    ros_4wd_driver::drive_telemetry_4wd drive;
    ros_4wd_driver::sensors_telemetry_4wd sensors;
    ros_4wd_driver::imu_raw_data imu;

    if( res = serial.waitInput(500) ) {
        //printf("[i] waitInput: %d\n", res);
        if( (res = serial.available()) > 0 ) {
            //printf("[i] available: %d\n", res);
            //res = res > (buff.real_size-buff.size) ? (buff.real_size-buff.size) : res ;
            if( (res = serial.read(buff.data+buff.size, buff.real_size-buff.size)) > 0 ) {
                buff.size += res;

                // get packet from data
                while ( (res = orcp2::get_packet(buff.data, buff.size, &pkt)) > 0) {
                    //     ROS_INFO("[i] res: %02d message: %04X size: %04d CRC: %02X buff.size: %02d\n",
                    //              res, pkt.message_type, pkt.message_size, pkt.checksum, buff.size);

                    uint8_t crc = pkt.calc_checksum();
                    if(crc == pkt.checksum) {
                        //         ROS_INFO("[i] Good message CRC: %02X\n", pkt.checksum);

                        current_time = ros::Time::now();

                        switch(pkt.message_type) {
                        case ORCP2_SEND_STRING:
                            pkt.message.data[pkt.message.size]=0;
                            ROS_INFO("[i] String: %s\n", pkt.message.data);
                            //printf("[i] counter: %d\n", counter);
#if 0
                            if(++counter > 50) {
                                //printf("[i] digitalWrite: %d\n", val);
                                val = !val;
                                orcp.digitalWrite(13, val);
                                counter = 0;
                            }
#endif
                            break;
                        case ORCP2_MESSAGE_IMU_RAW_DATA:
                            deserialize_imu3_data(pkt.message.data, pkt.message.size, &raw_imu_data);
                            ROS_INFO( "[i] IMU: acc: [%d %d %d] mag: [%d %d %d] gyro: [%d %d %d]\n",
                                      raw_imu_data.Accelerometer[0], raw_imu_data.Accelerometer[1], raw_imu_data.Accelerometer[2],
                                      raw_imu_data.Magnetometer[0], raw_imu_data.Magnetometer[1], raw_imu_data.Magnetometer[2],
                                      raw_imu_data.Gyro[0], raw_imu_data.Gyro[1], raw_imu_data.Gyro[2] );

                            imu.header.stamp = current_time;
                            imu.accelerometer1 = raw_imu_data.Accelerometer[0];
                            imu.accelerometer2 = raw_imu_data.Accelerometer[1];
                            imu.accelerometer3 = raw_imu_data.Accelerometer[2];
                            imu.magnetometer1 = raw_imu_data.Magnetometer[0];
                            imu.magnetometer2 = raw_imu_data.Magnetometer[1];
                            imu.magnetometer3 = raw_imu_data.Magnetometer[2];
                            imu.gyro1 = raw_imu_data.Gyro[0];
                            imu.gyro2 = raw_imu_data.Gyro[1];
                            imu.gyro3 = raw_imu_data.Gyro[2];

                            imu_raw_data_pub.publish(imu);
                            break;
                        case ORCP2_MESSAGE_ROBOT_4WD_DRIVE_TELEMETRY:
                            deserialize_robot_4wd_drive_part(pkt.message.data, pkt.message.size, &robot_data);
                            ROS_INFO( "[i] Drive telemetry: bmp: %d enc: [%d %d %d %d] PWM: [%d %d %d %d]\n",
                                      robot_data.Bamper,
                                      robot_data.Encoder[0], robot_data.Encoder[1], robot_data.Encoder[2], robot_data.Encoder[3],
                                      robot_data.PWM[0], robot_data.PWM[1], robot_data.PWM[2], robot_data.PWM[3] );

                            drive.header.stamp = current_time;
                            drive.bamper = robot_data.Bamper;
                            drive.encoder1 = robot_data.Encoder[0];
                            drive.encoder2 = robot_data.Encoder[1];
                            drive.encoder3 = robot_data.Encoder[2];
                            drive.encoder4 = robot_data.Encoder[3];
                            drive.pwm1 = robot_data.PWM[0];
                            drive.pwm2 = robot_data.PWM[1];
                            drive.pwm3 = robot_data.PWM[2];
                            drive.pwm4 = robot_data.PWM[3];

                            drive_telemetry_pub.publish(drive);
                            calc_odometry(drive);
                            break;
                        case ORCP2_MESSAGE_ROBOT_4WD_SENSORS_TELEMETRY:
                            deserialize_robot_4wd_sensors_part(pkt.message.data, pkt.message.size, &robot_data);
                            ROS_INFO( "[i] Sensors telemetry: US: %d IR: [%d %d %d %d] V: %d\n",
                                      robot_data.US[0],
                                      robot_data.IR[0], robot_data.IR[1], robot_data.IR[2], robot_data.IR[3],
                                      robot_data.Voltage );

                            sensors.header.stamp = current_time;
                            sensors.us = robot_data.US[0];
                            sensors.ir1 = robot_data.IR[0];
                            sensors.ir2 = robot_data.IR[1];
                            sensors.ir3 = robot_data.IR[2];
                            sensors.ir4 = robot_data.IR[3];
                            sensors.voltage = robot_data.Voltage;

                            sensors_telemetry_pub.publish(sensors);
                            break;
                        case ORCP2_MESSAGE_ROBOT_4WD_TELEMETRY:
                            deserialize_robot_4wd(pkt.message.data, pkt.message.size, &robot_data);
                            ROS_INFO( "[i] Drive Telemetry: bmp: %d enc: [%d %d %d %d] PWM: [%d %d %d %d]\n",
                                      robot_data.Bamper,
                                      robot_data.Encoder[0], robot_data.Encoder[1], robot_data.Encoder[2], robot_data.Encoder[3],
                                      robot_data.PWM[0], robot_data.PWM[1], robot_data.PWM[2], robot_data.PWM[3] );

                            drive.header.stamp = current_time;
                            drive.bamper = robot_data.Bamper;
                            drive.encoder1 = robot_data.Encoder[0];
                            drive.encoder2 = robot_data.Encoder[1];
                            drive.encoder3 = robot_data.Encoder[2];
                            drive.encoder4 = robot_data.Encoder[3];
                            drive.pwm1 = robot_data.PWM[0];
                            drive.pwm2 = robot_data.PWM[1];
                            drive.pwm3 = robot_data.PWM[2];
                            drive.pwm4 = robot_data.PWM[3];

                            drive_telemetry_pub.publish(drive);
                            calc_odometry(drive);

                            ROS_INFO( "[i] Sensors Telemetry: US: %d IR: [%d %d %d %d] V: %d\n",
                                      robot_data.US[0],
                                      robot_data.IR[0], robot_data.IR[1], robot_data.IR[2], robot_data.IR[3],
                                      robot_data.Voltage );

                            sensors.header.stamp = current_time;
                            sensors.us = robot_data.US[0];
                            sensors.ir1 = robot_data.IR[0];
                            sensors.ir2 = robot_data.IR[1];
                            sensors.ir3 = robot_data.IR[2];
                            sensors.ir4 = robot_data.IR[3];
                            sensors.voltage = robot_data.Voltage;

                            sensors_telemetry_pub.publish(sensors);
                            break;
                        default:
                            ROS_INFO("[i] Unknown message type: %04X!\n", pkt.message_type);
                            break;
                        }
                    }
                    else {
                        ROS_WARN("[!] Bad message CRC: %02X vs: %02X\n", crc, pkt.checksum);
                    }
                }
            }
            else {
                ROS_WARN("[!] too much data: %d (%d)\n", res, buff.size);
                buff.size = 0;
            }
        }
        else {
            ROS_INFO("[!] no available: %d\n", res);
        }
    }
    else {
        ROS_INFO("[!] waitInput timeout: %d\n", res);
    }

}