Publish servo messages on the serial for control of rc car with a arduino
I am trying to subscribe to messages which is being published on a topic (/ecu ) which has the steering and the servo input. I am getting an error [WARN] [WallTime: 1469219387.121113] Serial Port read returned short (expected 8 bytes, received 4 instead). [WARN] [WallTime: 1469219387.121657] Serial Port read failure: my code for publishing to the topic /ecu is :
import rospy
from barc.msg import ECU
def main_auto():
# initialize ROS node
rospy.init_node('auto_mode', anonymous=True)
nh = rospy.Publisher('ecu', ECU, queue_size = 10)
# set node rate
rateHz = 50
dt = 1.0 / rateHz
rate = rospy.Rate(rateHz)
t_i = 0
# main loop
while not rospy.is_shutdown():
# get steering wheel command
servoCMD = 96.0
motorCMD = 92.0
# send command signal
ecu_cmd = ECU(motorCMD, servoCMD)
nh.publish(ecu_cmd)
# wait
t_i += 1
rate.sleep()
#############################################################
if __name__ == '__main__':
try:
main_auto()
except rospy.ROSInterruptException:
pass
the arduino code i have used it from the open source BARC-project
/* ---------------------------------------------------------------------------
# Licensing Information: You are free to use or extend these projects for
# education or reserach purposes provided that (1) you retain this notice
# and (2) you provide clear attribution to UC Berkeley, including a link
# to http://barc-project.com
#
# Attibution Information: The barc project ROS code-base was developed
# at UC Berkeley in the Model Predictive Control (MPC) lab by Jon Gonzales
# (jon.gonzales@berkeley.edu) Development of the web-server app Dator was
# based on an open source project by Bruce Wootton, with contributions from
# Kiet Lam (kiet.lam@berkeley.edu). The RC Input code was based on sample code
# from http://rcarduino.blogspot.com/2012/04/how-to-read-multiple-rc-channels-draft.html
# --------------------------------------------------------------------------- */
/* ---------------------------------------------------------------------------
WARNING:
* Be sure to have all ultrasound sensors plugged in, otherwise the pins may get stuck in
some float voltage
# --------------------------------------------------------------------------- */
// include libraries
#include <ros.h>
#include <barc/Ultrasound.h>
#include <barc/Encoder.h>
#include <barc/ECU.h>
#include <Servo.h>
#include "Maxbotix.h"
#include <EnableInterrupt.h>
/**************************************************************************
CAR CLASS DEFINITION (would like to refactor into car.cpp and car.h but can't figure out arduino build process so far)
**************************************************************************/
class Car {
public:
void initEncoders();
void initRCInput();
void initActuators();
void armActuators();
void writeToActuators(const barc::ECU& ecu);
// Used for copying variables shared with interrupts to avoid read/write
// conflicts later
void readAndCopyInputs();
// Getters
uint8_t getRCThrottle();
uint8_t getRCSteering();
int getEncoderFL();
int getEncoderFR();
int getEncoderBL();
int getEncoderBR();
// Interrupt service routines
void incFR();
void incFL();
void incBR();
void incBL();
void calcThrottle();
void calcSteering();
private:
// Pin assignments
const int ENC_FL_PIN = 2;
const int ENC_FR_PIN = 3;
const int ENC_BL_PIN = 5;
const int ENC_BR_PIN = 6;
const int THROTTLE_PIN = 7;
const int STEERING_PIN = 8;
const int MOTOR_PIN = 10;
const int SERVO_PIN= 11;
// Car properties
// unclear what this is for
const int noAction = 0;
// Motor limits
// TODO are these the real limits?
const int MOTOR_MAX = 120;
const int MOTOR_MIN = 40;
const int MOTOR_NEUTRAL = 90;
// Optional: smaller values for testing safety
/* const int MOTOR_MAX = 100; */
/* const int MOTOR_MIN = 75; */
// Steering limits
// TODO seems to me that the permissible steering range is really about [58,
// 120] judging from the sound of the servo pushing beyond a mechanical limit
// outside that range. The ...
I don't see any use of a serial port in your code, so I don't really understand how you're getting serial port errors.
This seems like an issue with the rosserial + arduino code that you're running. It's hard to see what's wrong from the code you've posted, but the BARC Arduino readme says that serial communication may not work if the Sonar is not connected.
P.S. - Instead of pasting the code you're using, it's better to provide a link to the project or the source code on github. It gives more context and it gives credit to the original author.
ok. I am new to to ros and programming in general. https://github.com/BARCproject/barc -this is the link to the project.
In the code, https://github.com/BARCproject/barc/b... (motorCMD, _) = test_mode(opt, rateHz, t_i)
when I change this to motorCMD = 96, the car doesn't run.
I can't find that line in the code you've linked to, and in general I have no idea what is wrong. As a general troubleshooting strategy, start by trying to run the sample before modifying it. If the sample works, then you know the error is in your modifications.