5.3. API - Library - Sensors¶
5.3.1. LightSensor¶
- class easysensors.LightSensor(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
AnalogSensorClass for the old Grove Light Sensor.
This class derives from
AnalogSensorclass, so all of its attributes and methods are inherited. For creating aLightSensorobject we need to callinit_light_sensor()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a LightSensor object through the gpg3_obj object light_sensor = gpg3_obj.init_light_sensor() # do the usual stuff, like read the data of the sensor value = light_sensor.read() value_percentage = light_sensor.percent_read() # take a look at AnalogSensor class for more methods and attributes
Or if we need to specify the port we want to use, we might do it like in the following example.
# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the Light Sensor connected to port = "AD2" light_sensor = gpg3_obj.init_light_sensor(port) # read the sensor the same way as in the previous example
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
LightSensorobject for the Grove Light Sensor.- Parameters:
"AD1" (str port =) – Port to which we have the Grove Light Sensor connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aLightSensorobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
5.3.2. SoundSensor¶
- class easysensors.SoundSensor(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
AnalogSensorClass for the Grove Sound Sensor.
This class derives from
AnalogSensorclass, so all of its attributes and methods are inherited. For creating aSoundSensorobject we need to callinit_sound_sensor()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a SoundSensor object through the gpg3_obj object sound_sensor = gpg3_obj.init_sound_sensor() # do the usual stuff, like read the data of the sensor value = sound_sensor.read() value_percentage = sound_sensor.percent_read() # take a look at AnalogSensor class for more methods and attributes
Or if we need to specify the port we want to use, we might do it like in the following example.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the sound sensor connected to port = "AD1" sound_sensor = gpg3_obj.init_sound_sensor(port) # read the sensor the same way as in the previous example
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
SoundSensorobject for the Grove Sound Sensor.- Parameters:
"AD1" (str port =) – Port to which we have the Grove Sound Sensor connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aSoundSensorobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
5.3.3. LoudnessSensor¶
- class easysensors.LoudnessSensor(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
AnalogSensorClass for the Grove Loudness Sensor.
This class derives from
AnalogSensorclass, so all of their attributes and methods are inherited. For creating aLoudnessSensorobject we need to callinit_loudness_sensor()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a LoudnessSensor object through the gpg3_obj object loudness_sensor = gpg3_obj.init_loudness_sensor() # do the usual stuff, like read the data of the sensor value = loudness_sensor.read() value_percentage = loudness_sensor.percent_read() # take a look at AnalogSensor class and Sensor class for more methods and attributes
Or if we need to specify the port we want to use, we might do it like in the following example.
# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the sound sensor connected to port = "AD1" loudness_sensor = gpg3_obj.init_loudness_sensor(port) # read the sensor the same way as in the previous example
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
LoudnessSensorobject for the Grove Loudness Sensor.- Parameters:
"AD1" (str port =) – Port to which we have the Grove Loudness Sensor connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aLoudnessSensorobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
5.3.4. UltrasonicSensor¶
- class easysensors.UltraSonicSensor(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
AnalogSensorClass for the Grove Ultrasonic Sensor.
This class derives from
AnalogSensorclass, so all of its attributes and methods are inherited. For creating aUltraSonicSensorobject we need to callinit_ultrasonic_sensor()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a UltraSonicSensor object through the gpg3_obj object ultrasonic_sensor = gpg3_obj.init_ultrasonic_sensor() # do the usual stuff, like read the distance the sensor is measuring distance_cm = ultrasonic_sensor.read() distance_inches = ultrasonic_sensor.read_inches() # take a look at AnalogSensor class for more methods and attributes
Or if we need to specify the port we want to use, we might do it like in the following example.
# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the ultrasonic sensor connected to port = "AD1" ultrasonic_sensor = gpg3_obj.init_ultrasonic_sensor(port) # read the sensor's measured distance as in the previous example
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
UltraSonicSensorobject for the Grove Ultrasonic Sensor.- Parameters:
"AD1" (str port =) – Port to which we have the Grove Ultrasonic Sensor connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aUltraSonicSensorobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
- is_too_close()[source]¶
Checks whether the Grove Ultrasonic Sensor measures a distance that’s too close to a target than what we consider a safe distance.
- Returns:
Whether the Grove Ultrasonic Sensor is too close from a target.
- Return type:
- Raises:
gopigo3.SensorError – If a sensor is not yet configured when trying to read it.
A safe distance can be set with the
set_safe_distance()method.Note
The default safe distance is set at 50 cm.
- set_safe_distance(dist)[source]¶
Sets a safe distance for the Grove Ultrasonic Sensor.
- Parameters:
dist (int) – Minimum distance from a target that we can call a safe distance.
To check whether the robot is too close from a target, please check the
is_too_close()method.Note
The default safe distance is set at 50 cm.
- get_safe_distance()[source]¶
Gets what we call the safe distance for the Grove Ultrasonic Sensor.
- Returns:
The minimum distance from a target that can be considered a safe distance.
- Return type:
Note
The default safe distance is set at 50 cm.
- read_mm()[source]¶
Measures the distance from a target in millimeters.
- Returns:
The distance from a target in millimeters.
- Return type:
- Raises:
gopigo3.ValueError – If trying to read an invalid value.
Exception – If any other error occurs.
Important
This method can read distances between 15-4300 millimeters.
This method will read the data for 3 times and it’ll discard anything that’s smaller than 15 millimeters and bigger than 4300 millimeters.
If data is discarded 5 times (due to a communication error with the sensor), then the method returns 5010.
- read()[source]¶
Measures the distance from a target in centimeters.
- Returns:
The distance from a target in centimeters.
- Return type:
Important
This method can read distances between 2-430 centimeters.
If data is discarded 5 times (due to a communication error with the sensor), then the method returns 501.
- read_inches()[source]¶
Measures the distance from a target in inches.
- Returns:
The distance from a target in inches.
- Return type:
float (one decimal)
Important
This method can read distances of up to 169 inches.
If data is discarded 5 times (due to a communication error with the sensor), then the method returns 501.
5.3.5. Buzzer¶
- class easysensors.Buzzer(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
AnalogSensorClass for the Grove Buzzer.
This class derives from
AnalogSensorclass, so all of its attributes and methods are inherited. For creating aBuzzerobject we need to callinit_buzzer()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a UltraSonicSensor object through the gpg3_obj object buzzer = gpg3_obj.init_buzzer() # turn on and off the buzzer buzzer.sound_on() sleep(1) buzzer.sound_off() # take a look at AnalogSensor class for more methods and attributes
If we need to specify the port we want to use, we might do it like in the following example.
# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the ultrasonic sensor connected to port = "AD1" buzzer = gpg3_obj.init_buzzer(port)
See also
For more sensors, please see our Dexter Industries shop.
- scale = {'A3': 220, 'A3#': 233, 'A4': 440, 'A4#': 466, 'B3': 247, 'B4': 494, 'C4': 261, 'C4#': 277, 'C5': 523, 'C5#': 554, 'D4': 293, 'D4#': 311, 'D5': 587, 'D5#': 622, 'E4': 329, 'E5': 659, 'F4': 349, 'F4#': 370, 'F5': 698, 'F5#': 740, 'G4': 392, 'G4#': 415, 'G5': 784, 'G5#': 831}¶
Dictionary of frequencies for each musical note. For instance,
scale["A3"]instruction is equal to 220 Hz (that’s the A3 musical note’s frequency). This dictionary is useful when we want to make the buzzer ring at certain frequencies (aka musical notes).
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
Buzzerobject for the Grove Buzzer.- Parameters:
"AD1" (str port =) – Port to which we have the Grove Buzzer connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aBuzzerobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Variables:
50 (int power =) – Duty cycle of the signal that’s put on the buzzer.
329 (int freq =) – Frequency of the signal that’s put on the buzzer. 329Hz is synonymous to E4 musical note. See
scalefor more musical notes.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
- sound(freq)[source]¶
Sets a musical note for the Grove Buzzer.
- Parameters:
freq (int) – The frequency of the signal that’s put on the Grove Buzzer.
For a list of musical notes, please see
scale. See this example script on how to play musical notes.# initialize all the required objects and connect the sensor to the GoPiGo3 musical_notes = buzzer.scale notes_i_want_to_play = {"F4#", "F4#", "C5#", "B3", "B3", "B3"} wait_time = 1.0 for note in notes_i_want_to_play: buzzer.sound(musical_notes[note]) sleep(wait_time) # enjoy the musical notes
- sound_off()[source]¶
Turns off the Grove Buzzer.
- sound_on()[source]¶
Turns on the Grove Buzzer at the set frequency.
For changing the frequency, please check the
sound()method.
5.3.6. Led¶
- class easysensors.Led(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
AnalogSensorClass for the Grove LED.
With this class the following things can be done:
Turn ON/OFF an LED.
Set a level of brightness for the LED.
Check if an LED is turned ON or OFF.
This class derives from
AnalogSensorclass, so all of its attributes and methods are inherited. For creating aLedobject we need to callinit_led()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a Led object through the gpg3_obj object led = gpg3_obj.init_led() # turn on and off the buzzer led.light_max() sleep(1) led.light_off() # take a look at AnalogSensor class for more methods and attributes
If we need to specify the port we want to use, we might do it like in the following example.
# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the led connected to port = "AD1" led = gpg3_obj.init_led(port) # call some Led-specific methods
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
Ledobject for the Grove LED.- Parameters:
"AD1" (str port =) – Port to which we have the Grove LED connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aLedobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
- light_on(power)[source]¶
Sets the duty cycle for the Grove LED.
- Parameters:
power (int) – Number between 0 and 100 that represents the duty cycle of PWM signal.
5.3.7. MotionSensor¶
- class easysensors.MotionSensor(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
DigitalSensorClass for the Grove Motion Sensor.
This class derives from
Sensor(check for throwable exceptions) andDigitalSensorclasses, so all attributes and methods are inherited. For creating aMotionSensorobject we need to callinit_motion_sensor()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now instantiate a Motion Sensor object through the gpg3_obj object on default port AD1 motion_sensor = gpg3_obj.init_motion_sensor() while True: if motion_sensor.motion_detected(): print("motion detected") else: print("no motion") # take a look at DigitalSensor & Sensor class for more methods and attributes
If we need to specify the port we want to use, we might do it like in the following example.
# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # variable for holding the port to which we have the motion sensor connected to port = "AD2" motion_sensor = gpg3_obj.init_motion_sensor(port)
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
MotionSensorobject for the Grove Motion Sensor.- Parameters:
"AD1" (str port =) – Port to which we have the Grove Motion Sensor connected to.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object used for instantiating aMotionSensorobject.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
portparameter can take the following values:"AD1"- general purpose input/output port."AD2"- general purpose input/output port.
The ports’ locations can be seen in the following graphical representation: Hardware Ports.
- motion_detected(port='AD1')[source]¶
Checks if the Grove Motion Sensor detects a motion.
- Returns:
TrueorFalse, if the Grove Motion Sensor detects a motion or not.- Return type:
5.3.9. Servo¶
- class easysensors.Servo(port='SERVO1', gpg=None, use_mutex=False)[source]¶
Bases:
SensorClass for controlling servo motors with the GoPiGo3 robot. Allows you to rotate the servo by serving the angle of rotation.
This class is derived from
Sensorclass and because of this, it inherits all the attributes and methods.For creating a
Servoobject we need to callinit_servo()method like in the following examples.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now let's instantiate a Servo object through the gpg3_obj object # this will bind a servo to port "SERVO1" servo = gpg3_obj.init_servo() # rotate the servo at 160 degrees servo.rotate_servo(160)
Or if we want to specify the port to which we connect the servo, we need to call
init_servo()the following way.servo = gpg3_obj.init_servo("SERVO2")
See also
For more sensors, please see our Dexter Industries shop.
- __init__(port='SERVO1', gpg=None, use_mutex=False)[source]¶
Constructor for instantiating a
Servoobject for a (or multiple) servo (servos).- Parameters:
"SERVO1" (str port =) – The port to which we have connected the servo.
None (easygopigo3.EasyGoPiGo3 gpg =) –
EasyGoPiGo3object that we need for instantiation.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The available ports that can be used for a servo are:
"SERVO1"- servo controller port."SERVO2"- servo controller port.
To see where these 2 ports are located, please take a look at the following graphical representation: Hardware Ports.
- rotate_servo(servo_position)[source]¶
Rotates the servo at a specific angle.
- Parameters:
servo_position (int) – Angle at which the servo has to rotate. The values can be anywhere from 0 to 180 degrees.
The pulse width varies the following way:
575 uS for 0 degrees - the servo’s default position.
24250 uS for 180 degrees - where the servo is rotated at its maximum position.
Each rotation of 1 degree requires an increase of the pulse width by 10.27 uS.
Warning
We use PWM signals (Pulse Width Modulation), so the angle at which a servo will rotate will be case-dependent.This means a servo’s 180 degrees position won’t be the same as with another servo.
- reset_servo()[source]¶
Resets the servo straight ahead, in the middle position.
Tip
Same as callingrotate_servo(90).Read more aboutrotate_servo()method.
5.3.10. DHTSensor¶
- class easysensors.DHTSensor(gpg=None, sensor_type=0, use_mutex=False)[source]¶
Bases:
SensorClass for interfacing with the Grove DHT Sensor. This class derives from
Sensorclass, so all of its attributes and methods are inherited.We can create a
DHTSensorobject similar to how we create it in the following template.# create an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # and now let's instantiate a DHTSensor object through the gpg3_obj object dht_sensor = gpg3_obj.init_dht_sensor() # read values continuously and print them in the terminal while True: temp, hum = dht_sensor.read() print("temp = {:.1f} hum = {:.1f}".format(temp, hum))
- __init__(gpg=None, sensor_type=0, use_mutex=False)[source]¶
Constructor for creating a
DHTSensorobject which can be used for interfacing with the Grove DHT Sensor.- Parameters:
None (easygopigo3.EasyGoPiGo3 gpg =) – Object that’s required for instantianting a
DHTSensorobject.0 (int sensor_type =) – Choose
sensor_type = 0when you have the blue-coloured DHT sensor orsensor_type = 1when it’s white.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes have to be used.
- Raises:
Any of the
Sensorconstructor’s exceptions in case of error.
- read_temperature()[source]¶
Return the temperature in Celsius degrees.
- Returns:
The temperature in Celsius degrees.
- Return type:
If the sensor isn’t plugged in, a
"Bad reading, try again"message is returned. If there is a runtime error, then a"Runtime error"message is returned.
- read_humidity()[source]¶
Return the humidity as a percentage.
- Returns:
Return the humidity as a percentage number from 0% to 100%.
- Return type:
If the sensor isn’t plugged in, a
"Bad reading, try again"message is returned. If there is a runtime error, then a"Runtime error"message is returned.
- read()[source]¶
Return the temperature and humidity.
- Returns:
The temperature and humidity as a tuple, where the temperature is the 1st element of the tuple and the humidity the 2nd.
- Return type:
If the sensor isn’t plugged in, a
"Bad reading, try again"message is returned. If there is a runtime error, then a"Runtime error"message is returned.
5.3.11. Remote¶
- class easysensors.Remote(port='AD1', gpg=None, use_mutex=False)[source]¶
Bases:
SensorClass for interfacing with the Infrared Receiver.
With this sensor, you can command your GoPiGo3 with an Infrared Remote.
In order to create an object of this class, we would do it like in the following example.
# initialize an EasyGoPiGo3 object gpg3_obj = EasyGoPiGo3() # now initialize a Remote object remote_control = gpg3_obj.init_remote() # read whatever command you want from the remote by using # the [remote_control] object
- keycodes = ['up', 'left', 'ok', 'right', 'down', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*', '0', '#']¶
List for mapping the codes we get with the
read()method to the actual symbols we see on the Infrared Remote.
- __init__(port='AD1', gpg=None, use_mutex=False)[source]¶
Constructor for initializing a
Remoteobject.- Parameters:
"AD1" (str port =) – The port to which we connect the Infrared Receiver.
None (easygopigo3.EasyGoPiGo3 gpg =) – The
EasyGoPiGo3object that we need for instantiating this object.False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
TypeError – If the
gpgparameter is not aEasyGoPiGo3object.
The
"AD1"and"AD2"ports’ location on the GoPiGo3 robot can be seen in the following graphical representation: Hardware Ports.
- read()[source]¶
Reads the numeric code received by the Infrared Receiver.
- Returns:
The numeric code of the symbol that was pressed on the Infrared Remote.
- Return type:
The numeric code represents the index of the
keycodeslist. By accessing thekeycodeselements with the numeric code, you get the symbol that was pressed on the Infrared Remote.For only getting the symbol that was pressed on the Infrared Remote, please check the
get_remote_code()method.Warning
On
SensorErrorexception:"Invalid Reading"string is printed in the console.The value of -1 is returned.
- get_remote_code()[source]¶
Returns the symbol of the pressed key in a string format.
- Returns:
The symbol that was pressed on the Infrared Remote.
- Return type:
Check the
keycodeslist for seeing what strings this method can return. On error or when nothing is read, an empty string is returned.
5.3.12. LineFollower¶
- easysensors.LineFollower(port='I2C', gpg=None, use_mutex=False)[source]¶
Returns an instantiated object of
di_sensors.easy_line_follower.EasyLineFollower.This is a replacement function for the line follower class that used to be found here - it has not moved to
di_sensorslibrary.- Parameters:
"I2C" (str port =) – The port to which we have connected the line follower sensor. Can also be
"AD1"/"AD2"for the black Line Follower sensor.None (easygopigo3.EasyGoPiGo3 gpg =) – This is no longer required. Just skip this parameter.
False (bool use_mutex =) – When using multiple threads/processes that access the same resource/device, mutexes should be enabled.
- Raises:
ImportError – If the
di_sensorslibrary couldn’t be found.IOError – If the line follower is not responding.