First, please don't ask the same question twice. It doesn't get answered faster, and might cause some people to disregard your question as being impolite. Better to edit your original question than ask a duplicate question with different wording.
There is more than one way to lay out your logic among ROS nodes. Perhaps the most ROS-like way might be this:
- One node that reads the range sensor and publishes the range as a ROS
sensor_msgs/Range
message. - A decision node that reads the
Range
message and publishes another message to turn on or off the motor, perhaps as std_msgs/Bool
. - A third node that listens for the
Bool
message and turns on or off the motor.
This arrangement has the advantage that input nodes, output nodes, and logic nodes are distinct and independent.
However, you might decide to collapse these into two or one node to be faster or more efficient. I don't know what software you are using on the Arduino side. I have used both rosserial_arduino
and ros_arduino_bridge
. rosserial
makes programming on the Arduino side more ROS-like, but has fairly high RAM usage, so is more suitable for mega-class Arduinos (or Teensy, for example). ros_arduino_bridge
may require you to modify the code to accept a new command to turn on or off the motor. (It has support for some motor shields.) If you give some more details, I may be able to point you to forks off of ros_arduino_bridge
that do something like you want.