ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

create python script for infrared sensor

asked 2022-06-04 11:19:58 -0600

RoboTBiLL gravatar image

I have an experimental robot with five old Sharp infrared distance sensors.

I found this code and did some changes in order to add five sensors.

#!/usr/bin/python
import spidev

spi = spidev.SpiDev()
spi.open(0,0)
spi.max_speed_hz=1000000


def readChannel(channel):
  val = spi.xfer2([1,(8+channel)<<4,0])
  data = ((val[1]&3) << 8) + val[2]
  return data

if __name__ == "__main__":
  v1=(readChannel(0)/1023.0)*3.3
  dist1 = 16.2537 * v1**4 - 129.893 * v1**3 + 382.268 * v1**2 - 512.611 * v1 + 301.439
  print "Distance: %.2f cm" % dist1

  v2=(readChannel(1)/1023.0)*3.3
  dist2 = 16.2537 * v2**4 - 129.893 * v2**3 + 382.268 * v2**2 - 512.611 * v2 + 301.439
  print "Distance: %.2f cm" % dist2

  v3=(readChannel(2)/1023.0)*3.3
  dist3 = 16.2537 * v3**4 - 129.893 * v3**3 + 382.268 * v3**2 - 512.611 * v3 + 301.439
  print "Distance: %.2f cm" % dist3

  v4=(readChannel(3)/1023.0)*3.3
  dist4 = 16.2537 * v4**4 - 129.893 * v4**3 + 382.268 * v4**2 - 512.611 * v4 + 301.439
  print "Distance: %.2f cm" % dist4

  v5=(readChannel(4)/1023.0)*3.3
  dist5 = 16.2537 * v5**4 - 129.893 * v5**3 + 382.268 * v5**2 - 512.611 * v5 + 301.439
  print "Distance: %.2f cm" % dist5

Is there a way to do a ROS python script in order to communicate between the others scripts?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-06-04 13:00:36 -0600

Mix_MicDev gravatar image

I'm not sure if I understood your question, but if by communicating between other scripts you mean share the distances with other scripts, you could simply import rospy module, initialise a node, create a rospy.Publisher() to publish data to a Topic of your choice. Then you just subscribe to this Topic from the "other scripts" in order to get the data. Take a look at the ROS tutorial here to see how to create Publishers and Subscribers: http://wiki.ros.org/ROS/Tutorials/Wri...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-06-04 11:19:58 -0600

Seen: 225 times

Last updated: Jun 04 '22