Hi,

I need a little help ; I was sick so i just started today to try to use my ultrasonic sensor (SR04) with MRL but i didn't success because the function pulseIn isn't recognized by the arduino service  and i don't know how to do without it . can someone help me .

Grog ,i just seen on this page http://myrobotlab.org/content/attacknid-hack-robot-mrl that you speak about make a map of the environement . I'm very interested ! with my six sensors around the base of my robot, i must have a very good map ! 

thx

here is what i did :


from time import sleep

from org.myrobotlab.service import Arduino



# variables dependent on your setup
boardType = "atmega328"  # atmega168 | atmega328p | atmega2560 | atmega1280 | atmega32u4
comPort = "COM4" 
trigPin = 22
echoPin = 23

# create an Arduino service named arduino
arduino = runtime.createAndStart("arduino","Arduino")

# set the board type
arduino.setBoard(boardType) # atmega168 | mega2560 | etc

# set serial device
arduino.connect(comPort)
sleep(1) # give it a second for the serial device to get ready

# pins setup
arduino.pinMode(trigPin, arduino.OUTPUT);
arduino.pinMode(echoPin, arduino.INPUT);

# update the gui with configuration changes
arduino.publishState()

# calculate the distance
arduino.digitalWrite(trigPin, arduino.LOW) 
sleep(0.002); 
arduino.digitalWrite(trigPin, arduino.HIGH)
sleep(0.01); 
arduino.digitalWrite(trigPin, arduino.LOW)
duration = arduino.pulseIn(echoPin, arduino.HIGH) ERROR !

#Convert the distance (in cm) based on the speed of sound.
distance = duration/58.2

GroG

10 years 3 months ago

Beetlejuice !

Your back !

And I agree ! - We must build the Mapping !

I will implement pulseIn .. it will take a little time.  In the inerim can you post pictures of your robot, or of the 6 sensors you currently have?  Or do you have only 1 sensor?  In order to provide maximum help I need to know your setup.  

Currently, I'm still working on move from code.google.com to github.com :P  taking much longer than I expected, but right after I would like to do this & PIR.

beetlejuice

10 years 3 months ago

Here is some pictures wink

still miss the hands, and i must redo the head with the same wood than the body .

 

 

Front

 

Back

beetlejuice

10 years 3 months ago

Her is the code i'm testing without pulseIn, but the distance isn't right ...

import time
from time import sleep
from org.myrobotlab.service import Arduino



# variables dependent on your setup
boardType = "atmega2560"  # atmega168 | atmega328p | atmega2560 | atmega1280 | atmega32u4
comPort = "COM8" # com4 for atmega328 com8 for mega2560
trigPin = 22
echoPin = 23

# create an Arduino service named arduino
arduino = runtime.createAndStart("arduino","Arduino")

# set the board type
arduino.setBoard(boardType) # atmega168 | mega2560 | etc

# set serial device
arduino.connect(comPort)
sleep(1) # give it a second for the serial device to get ready

# pins setup
arduino.pinMode(trigPin, arduino.OUTPUT);
arduino.pinMode(echoPin, arduino.INPUT);

# update the gui with configuration changes
arduino.publishState()

# calculate the distance

# A couple of variables
# ---------------------
EXIT = 0                        # Infinite loop
pulsetrigger = 0.0001        # Trigger duration
timeout = 2100               # Number of loop iterations before timeout called


# Wait for 2 seconds to allow the ultrasonics to settle (probably not needed)
# ---------------------------------------------------------------------------
#print "Waiting for 2 seconds....."
#time.sleep(2)


# Go
# --
print "Running...."


# Never ending loop
# -----------------
while EXIT == 0:

        # Trigger high for 0.0001s then low
        
	arduino.digitalWrite(trigPin, arduino.HIGH)
	time.sleep(pulsetrigger)
	arduino.digitalWrite(trigPin, arduino.LOW)
	# Wait for echo to go high (or timeout)

	countdown = timeout

	while (arduino.digitalReadPollingStart(echoPin) == 0 and countdown > 0):
		countdown = countdown - 1

	# If echo is high

	if countdown > 0:

		# Start timer and init timeout countdown

		echostart = time.time()
		countdown = timeout

		# Wait for echo to go low (or timeout)

		while (arduino.digitalReadPollingStart(echoPin) == 1 and intcountdown > 0):
			countdown = countdown - 1

		# Stop timer
		arduino.digitalReadPollingStop(echoPin)
		echoend = time.time()


		# Echo duration

		echoduration = echoend - echostart
		print "duree  = " + str(echoduration)

	# Display distance

	if countdown > 0:
		distance = (echoduration*1000000)/58
		print "Distance = " + str(distance) + "cm"
	else:
		print "Distance - timeout"

        # Wait at least .01s before re trig (or in this case .1s)

        time.sleep(.1)