Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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 this issue Jul 7, 2022 · 1 comment

Comments

@Rothn12
Copy link

Rothn12 commented Jul 7, 2022

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:

/*

  • Software License Agreement (BSD License)
  • Copyright (c) 2011, Willow Garage, Inc.
  • All rights reserved.
  • Redistribution and use in source and binary forms, with or without
  • modification, are permitted provided that the following conditions
  • are met:
    • Redistributions of source code must retain the above copyright
  • notice, this list of conditions and the following disclaimer.
    • Redistributions in binary form must reproduce the above
  • copyright notice, this list of conditions and the following
  • disclaimer in the documentation and/or other materials provided
  • with the distribution.
    • Neither the name of Willow Garage, Inc. nor the names of its
  • contributors may be used to endorse or promote prducts derived
  • from this software without specific prior written permission.
  • THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  • "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  • LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  • FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  • COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  • INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  • BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  • LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  • CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  • LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  • ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  • POSSIBILITY OF SUCH DAMAGE.
    */

#ifndef ROSSERIAL_VEX_V5_V5_V5RS485_H
#define ROSSERIAL_VEX_V5_V5_V5RS485_H

#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
Copy link

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants