ros-drivers / rosserial

A ROS client library for small, embedded devices, such as Arduino. See: http://wiki.ros.org/rosserial
508 stars 527 forks source link

No support for serial communication over VEX V5 brain's RS485 ports in noetic-devel. I have the fix for that. #591

Open Rothn12 opened 1 year ago

Rothn12 commented 1 year ago

There is currently no support for serial communications over the VEX V5 brain's RS485 port. I do not have permissions to commit this fix, so here it is. I would suggest creating a new file called V5RS485.h and adding the following:

/*

ifndef _ROSSERIAL_VEX_V5_V5_V5RS485H

define _ROSSERIAL_VEX_V5_V5_V5RS485H

include "pros/apix.h"

class V5RS485 { public: V5RS485(int readPortNum = 19, int writePortNum = 20, int baud = 115200) { readPort = new pros::Serial(readPortNum); writePort = new pros::Serial(writePortNum); readPort->set_baudrate(baud); writePort->set_baudrate(baud); }

void init() {
    pros::delay(20);
    readPort->flush();
    pros::delay(100);
    writePort->flush();
}

// read a byte from the serial port. -1 = failure
int read() { 
    int32_t read = readPort->read_byte();
    if(read == PROS_ERR){
        std::cout << pros::millis() << " Error read " << read << std::endl;
    } 
    return read;
}

// write data to the connection to ROS
void write(uint8_t* data, int length) { 
    int freeBytes = writePort->get_write_free();
    if(freeBytes > length) { // Enough bytes free in buffer
        writePort->write(data, length);
        //This delay is necessary to prevent too many bytes being sent at once
        //This will prevent send errors
        //Without this the current scheduler will not properly space out the writes
        //A different solution to this issue would be to edit the scheduler to minimize no-ops
        pros::delay(2);
    } else {
        printf("Serial buffer full!\n");
        writePort->write(data, freeBytes);
        pros::delay(2);
        for(int i = freeBytes; i < length; i++) {
            while(writePort->get_write_free() == 0) {
                pros::delay(5);
            }
            writePort->write_byte(data[i]);
            pros::delay(3);
        }
    }
}

// returns milliseconds since start of program
unsigned long time() { return pros::c::millis(); }

private: //These need to be pointers pros::Serial readPort = nullptr; pros::Serial writePort = nullptr; };

endif

HBUTHUANGPX commented 1 year ago

RS485 just a physical layer for Serial port.If we have a RS485 to usb device ,then the usage as same as "ttl to usb"