From 01ee8c9b58ad2b691ca51c20cd9ca1c36564e5d0 Mon Sep 17 00:00:00 2001 From: Lorenz Meier Date: Mon, 19 Oct 2015 13:14:40 +0200 Subject: [PATCH] roboclaw: Fixed code style --- src/drivers/roboclaw/RoboClaw.cpp | 121 +++++++++++++++++-------- src/drivers/roboclaw/roboclaw_main.cpp | 25 ++--- 2 files changed, 97 insertions(+), 49 deletions(-) diff --git a/src/drivers/roboclaw/RoboClaw.cpp b/src/drivers/roboclaw/RoboClaw.cpp index 5b945e452d..a3d2f96a20 100644 --- a/src/drivers/roboclaw/RoboClaw.cpp +++ b/src/drivers/roboclaw/RoboClaw.cpp @@ -61,7 +61,7 @@ uint8_t RoboClaw::checksum_mask = 0x7f; RoboClaw::RoboClaw(const char *deviceName, uint16_t address, - uint16_t pulsesPerRev): + uint16_t pulsesPerRev): _address(address), _pulsesPerRev(pulsesPerRev), _uart(0), @@ -80,18 +80,27 @@ RoboClaw::RoboClaw(const char *deviceName, uint16_t address, // start serial port _uart = open(deviceName, O_RDWR | O_NOCTTY); - if (_uart < 0) err(1, "could not open %s", deviceName); + + if (_uart < 0) { err(1, "could not open %s", deviceName); } + int ret = 0; struct termios uart_config; ret = tcgetattr(_uart, &uart_config); - if (ret < 0) err (1, "failed to get attr"); + + if (ret < 0) { err(1, "failed to get attr"); } + uart_config.c_oflag &= ~ONLCR; // no CR for every LF ret = cfsetispeed(&uart_config, B38400); - if (ret < 0) err (1, "failed to set input speed"); + + if (ret < 0) { err(1, "failed to set input speed"); } + ret = cfsetospeed(&uart_config, B38400); - if (ret < 0) err (1, "failed to set output speed"); + + if (ret < 0) { err(1, "failed to set output speed"); } + ret = tcsetattr(_uart, TCSANOW, &uart_config); - if (ret < 0) err (1, "failed to set attr"); + + if (ret < 0) { err(1, "failed to set attr"); } // setup default settings, reset encoders resetEncoders(); @@ -107,25 +116,30 @@ RoboClaw::~RoboClaw() int RoboClaw::readEncoder(e_motor motor) { uint16_t sum = 0; + if (motor == MOTOR_1) { _sendCommand(CMD_READ_ENCODER_1, nullptr, 0, sum); + } else if (motor == MOTOR_2) { _sendCommand(CMD_READ_ENCODER_2, nullptr, 0, sum); } + uint8_t rbuf[50]; usleep(5000); int nread = read(_uart, rbuf, 50); + if (nread < 6) { printf("failed to read\n"); return -1; } + //printf("received: \n"); //for (int i=0;i 1) value = 1; - if (value < -1) value = -1; - uint8_t speed = fabs(value)*127; + if (value > 1) { value = 1; } + + if (value < -1) { value = -1; } + + uint8_t speed = fabs(value) * 127; + // send command if (motor == MOTOR_1) { if (value > 0) { return _sendCommand(CMD_DRIVE_FWD_1, &speed, 1, sum); + } else { return _sendCommand(CMD_DRIVE_REV_1, &speed, 1, sum); } + } else if (motor == MOTOR_2) { if (value > 0) { return _sendCommand(CMD_DRIVE_FWD_2, &speed, 1, sum); + } else { return _sendCommand(CMD_DRIVE_REV_2, &speed, 1, sum); } } + return -1; } int RoboClaw::setMotorDutyCycle(e_motor motor, float value) { uint16_t sum = 0; + // bound - if (value > 1) value = 1; - if (value < -1) value = -1; - int16_t duty = 1500*value; + if (value > 1) { value = 1; } + + if (value < -1) { value = -1; } + + int16_t duty = 1500 * value; + // send command if (motor == MOTOR_1) { return _sendCommand(CMD_SIGNED_DUTYCYCLE_1, - (uint8_t *)(&duty), 2, sum); + (uint8_t *)(&duty), 2, sum); + } else if (motor == MOTOR_2) { return _sendCommand(CMD_SIGNED_DUTYCYCLE_2, - (uint8_t *)(&duty), 2, sum); + (uint8_t *)(&duty), 2, sum); } + return -1; } @@ -247,7 +285,7 @@ int RoboClaw::resetEncoders() { uint16_t sum = 0; return _sendCommand(CMD_RESET_ENCODERS, - nullptr, 0, sum); + nullptr, 0, sum); } int RoboClaw::update() @@ -256,52 +294,57 @@ int RoboClaw::update() // check for exit condition every second // note "::poll" is required to distinguish global // poll from member function for driver - if (::poll(&_controlPoll, 1, 1000) < 0) return -1; // poll error + if (::poll(&_controlPoll, 1, 1000) < 0) { return -1; } // poll error // if new data, send to motors if (_actuators.updated()) { _actuators.update(); - setMotorDutyCycle(MOTOR_1,_actuators.control[CH_VOLTAGE_LEFT]); - setMotorDutyCycle(MOTOR_2,_actuators.control[CH_VOLTAGE_RIGHT]); + setMotorDutyCycle(MOTOR_1, _actuators.control[CH_VOLTAGE_LEFT]); + setMotorDutyCycle(MOTOR_2, _actuators.control[CH_VOLTAGE_RIGHT]); } + return 0; } -uint16_t RoboClaw::_sumBytes(uint8_t * buf, size_t n) +uint16_t RoboClaw::_sumBytes(uint8_t *buf, size_t n) { uint16_t sum = 0; + //printf("sum\n"); - for (size_t i=0;i