using motor and light sensor

584 views
Skip to first unread message

Murielle PIRON

unread,
May 10, 2011, 3:51:19 PM5/10/11
to nxt-python
Hi,

Could you help me to find the way to get informations from the light
sensor of my robot.

first question: I think that the light sensor is better fixed on port
3
But when I fix it on port 3, if I use test_sensors.py in examples
directory, here is the response:

========================
Touch: False
Sound: 0
Light: 0
Ultrasonic:
Traceback (most recent call last):
File "test_sensors.py", line 11, in <module>
print 'Ultrasonic:', Ultrasonic(b, PORT_4).get_sample()
File "/usr/local/lib/python2.6/dist-packages/nxt/sensor/generic.py",
line 95, in __init__
super(Ultrasonic, self).__init__(brick, port, check_compatible)
File "/usr/local/lib/python2.6/dist-packages/nxt/sensor/digital.py",
line 73, in __init__
sensor = self.get_sensor_info()
File "/usr/local/lib/python2.6/dist-packages/nxt/sensor/digital.py",
line 153, in get_sensor_info
version = self.read_value('version')[0].split('\0')[0]
File "/usr/local/lib/python2.6/dist-packages/nxt/sensor/digital.py",
line 140, in read_value
raise I2CError, "read_value timeout"
nxt.error.I2CError: read_value timeout
========================================

There are errors but the beginning tell that light sensor isn't
find... :(

If I go ahead this problem,
my goal is to follow a black line.
I think the script has to begin whith:


import nxt, nxt.sensor
b = nxt.find_one_brick()
sensor= nxt.Sensor(b,2)

Here is the result
b: <nxt.brick.Brick object at 0xb71a8f2c>
sensor: <nxt.sensor.common.Sensor object at 0xb71a8e8c>

Now, I need to turn on the sensor and get the intensity reflected on
the surface.
I don't find the way to do this.
Who can help me?


Thanks to read me.

Murielle

Marcus Wanner

unread,
May 10, 2011, 7:44:38 PM5/10/11
to nxt-p...@googlegroups.com
On Tue, May 10, 2011 at 12:51:19PM -0700, Murielle PIRON wrote:
> Hi,
>
> Could you help me to find the way to get informations from the light
> sensor of my robot.
>
> first question: I think that the light sensor is better fixed on port 3 But
> when I fix it on port 3, if I use test_sensors.py in examples directory,
> here is the response:

It doesn't really matter which point you use...port 4 apparently has some kind
of high-speed data bus available on it, but I don't think anything uses this
yet (and nxt-python doesn't know how to talk to it).

<snip>
> Light: 0
<snip>

Yeah, it's not reading the light sensor. Weird.

> There are errors but the beginning tell that light sensor isn't find... :(

The error is because the Ultrasonic Sensor is a digital sensor and will cause
an error if it's not connected. That's fine. I don't know why the light sensor
doesn't work, though...

> If I go ahead this problem, my goal is to follow a black line.

This is very hard, good luck!

> I think the script has to begin whith:
> import nxt, nxt.sensor
> b = nxt.find_one_brick()
> sensor= nxt.Sensor(b,2)

That should be:
sensor = nxt.Light(b, nxt.PORT_3)

> Now, I need to turn on the sensor and get the intensity reflected on the
> surface. I don't find the way to do this. Who can help me?

sensor.set_illuminated(True)

Maybe it will work after doing this? You should get values with:

sensor.get_sample()

--
Marcus Wanner

signature.asc

Murielle PIRON

unread,
May 11, 2011, 2:29:48 PM5/11/11
to nxt-python
Here is the program tested:

import nxt
b = nxt.find_one_brick()

sensor = nxt.Light(b, nxt.PORT_3)
print sensor
x=sensor.get_sample()
print x
sensor.set_illuminated(True)
x=sensor.get_sample()
print x


Here is my result:
<nxt.sensor.generic.Light object at 0xb7160eac>
1023
1023


The lights never illuminated
If I put the sensor on black or white surface, the "get_sample
response" ils always the same.
Conclusion: I'm not able to use it.

Murielle PIRON

unread,
May 11, 2011, 2:39:24 PM5/11/11
to nxt-python

> sensor = nxt.Light(b, nxt.PORT_3)
> Conclusion: I'm not able to use it.

On the other side, if I replace the light sensor by a touch sensor, it
works perfectly

==========
import nxt
b = nxt.find_one_brick(name = 'MyNXT')
sensor = nxt.Touch(b, nxt.PORT_3)
print sensor
x=sensor.get_sample()
print x
===========

When I don't press:
<nxt.sensor.generic.Touch object at 0xb71abeac>
False

When I press:
<nxt.sensor.generic.Touch object at 0xb70b0eac>
False

Marcus Wanner

unread,
May 11, 2011, 3:38:06 PM5/11/11
to nxt-p...@googlegroups.com
On Wed, May 11, 2011 at 11:29:48AM -0700, Murielle PIRON wrote:
> Here is the program tested:
>
> import nxt
> b = nxt.find_one_brick()
> sensor = nxt.Light(b, nxt.PORT_3)
> print sensor
> x=sensor.get_sample()
> print x
> sensor.set_illuminated(True)
> x=sensor.get_sample()
> print x

That's good. The program works for me.

> Here is my result:
> <nxt.sensor.generic.Light object at 0xb7160eac>
> 1023
> 1023

Strange. Usually, if it doesn't see the sensor, it returns 0.

> The lights never illuminated

This is also strange.

> If I put the sensor on black or white surface, the "get_sample response" ils
> always the same.

Yeah, it's not seeing the sensor for some reason.

> Conclusion: I'm not able to use it.

Does everything else you've tried with nxt-python work? If so, this may be a
hardware issue. Do other programs work with the light sensor? If so, please
add these lines to your prorgam just after "print sensor":

sensor.debug = True
b.debug = True
b.sock.debug = True

Sorry it's not working...

--
Marcus Wanner

signature.asc

Rochan Avlur

unread,
Mar 22, 2013, 6:24:58 AM3/22/13
to nxt-p...@googlegroups.com
How about a color sensor. what would be the code.

tjgil...@googlemail.com

unread,
Mar 9, 2014, 8:44:45 PM3/9/14
to nxt-p...@googlegroups.com
I struggled to find any sample code for using the NXT2 colour sensor so here is a suitable example I have called colour.py - it is different to the original sensor

#!/usr/bin/env python

import nxt.locator
from nxt.sensor import *
from time import sleep

b = nxt.locator.find_one_brick()

s = Color20(b, PORT_3)
s.set_light_color(Type.COLORGREEN)
print 'Hold touch sensor on port 1 to read reflected red light'
sleep(1)
if Touch(b, PORT_1).get_sample():
  s.set_light_color(Type.COLORRED)
  sleep(1)
  print 'Colour red reflected intensity is:', s.get_reflected_light(Type.COLORRED)
else:
  s.set_light_color(Type.COLORFULL)
  sleep(1)
  print 'Colour sampling RGB:', s.get_color()
s.set_light_color(Type.COLORNONE)
Reply all
Reply to author
Forward
0 new messages