NAOqi Sensors - Overview | API | Tutorial
This Python example shows how to retrieve the values in ALMemory. It gives the values of the total weight on both feet until foot contact is lost.
# -*- encoding: UTF-8 -*-
from naoqi import ALProxy
import time
import sys
def main(robotIP):
PORT = 9559
# Create proxy to ALMemory
try:
memoryProxy = ALProxy("ALMemory", robotIP, PORT)
except Exception, e:
print "Could not create proxy to ALMemory"
print "Error was: ", e
footContact = True
footContact = memoryProxy.getData("footContact")
while footContact:
footContact = memoryProxy.getData("footContact")
leftFoot = memoryProxy.getData("leftFootTotalWeight")
rightFoot = memoryProxy.getData("rightFootTotalWeight")
print ("Total weight on left foot: %.2f kg, on right foot: %.2f kg" % (leftFoot, rightFoot))
time.sleep(1.0)
print("Foot contact lost")
if __name__ == "__main__":
robotIp = "127.0.0.1"
if len(sys.argv) <= 1:
print "Usage python sensors_fsr.py robotIP (optional default: 127.0.0.1)"
else:
robotIp = sys.argv[1]
main(robotIp)
Another possible application is to implement a module with a callback on the event “footContactChanged” (see Event subscription for information on how to do so).
It is also possible to access sensors values directly, such as in the following example: sensors_getFsrValues.py.
# -*- encoding: UTF-8 -*-
from naoqi import ALProxy
import sys
def main(robotIP):
PORT = 9559
# Create proxy to ALMemory
try:
memoryProxy = ALProxy("ALMemory", robotIP, PORT)
except Exception, e:
print "Could not create proxy to ALMemory"
print "Error was: ", e
# Get The Left Foot Force Sensor Values
LFsrFL = memoryProxy.getData("Device/SubDeviceList/LFoot/FSR/FrontLeft/Sensor/Value")
LFsrFR = memoryProxy.getData("Device/SubDeviceList/LFoot/FSR/FrontRight/Sensor/Value")
LFsrBL = memoryProxy.getData("Device/SubDeviceList/LFoot/FSR/RearLeft/Sensor/Value")
LFsrBR = memoryProxy.getData("Device/SubDeviceList/LFoot/FSR/RearRight/Sensor/Value")
print( "Left FSR [Kg] : %.2f %.2f %.2f %.2f" % (LFsrFL, LFsrFR, LFsrBL, LFsrBR) )
# Get The Right Foot Force Sensor Values
RFsrFL = memoryProxy.getData("Device/SubDeviceList/RFoot/FSR/FrontLeft/Sensor/Value")
RFsrFR = memoryProxy.getData("Device/SubDeviceList/RFoot/FSR/FrontRight/Sensor/Value")
RFsrBL = memoryProxy.getData("Device/SubDeviceList/RFoot/FSR/RearLeft/Sensor/Value")
RFsrBR = memoryProxy.getData("Device/SubDeviceList/RFoot/FSR/RearRight/Sensor/Value")
print( "Right FSR [Kg] : %.2f %.2f %.2f %.2f" % (RFsrFL, RFsrFR, RFsrBL, RFsrBR) )
if __name__ == "__main__":
robotIp = "127.0.0.1"
if len(sys.argv) <= 1:
print "Usage python sensors_getFsrValues.py robotIP (optional default: 127.0.0.1)"
else:
robotIp = sys.argv[1]
main(robotIp)