[x y z]
represent the maximum distance, the maximum return value and the standard of noise respective.Firstly, add the “distancesensor” to the children part of the robot.
By modifying the “translation” and “rotation” property, we could change the relative position between the robot and the sensor. By modifying “rotation” property, we could change the direction towards which the sensor detect.
Noticing that, the sensor always detect along the positive direction of the x-axis.
Changing the “name” property of the sensor, and this will be the name that we will use in the ### python code.
ds = []
dsNames = ['ds_right', 'ds_left']
for i in range(2):
ds.append(robot.getDistanceSensor(dsNames[i]))
ds[i].enable(TIME_STEP)
...
if ds[0].getValue() > 950.0:
...
where robot.getDistanceSensor(dsNames[i])
will call the sensor and ds[0].getValue()
will give the distance that the sensor detect.
.getValue()
will return a float of 1000.0Firstly, add the “camera” to the children part of the robot.
The modifying process of the position and the direction is the same shown in above section.
The direction that the camera towards is the negative direction along the z-axis (opposite to the blue arrow).
the upper part of the camera is along the positive direction along the y-axis (towards the green arrow). The position of the camera that I select:
from controller import Robot,Camera
...
camera=Camera("test")
camera.enable(1)
...
image = camera.getImageArray()
Camera
package. and camera=Camera("camera")
calls the camera with a name property of “test”..enable(x)
, could the camera start to work. where x
means the camera will use x ms to take a picture..getImageArray()
, we could get a “list” the represent the last picture that the camera get. The channel is RGB.import numpy as np
import cv2
Applying .getImageArray()
, we could get a list that represent the picture.
image = np.array(camera.getImageArray(),dtype="uint8")
r,g,b=cv2.split(image)
image=cv2.merge((b,g,r))
int32
, which could not be processed by the opencv. In that case, we need to set dtype="uint8"
so that the opencv could process without error.Add a compass as the children of the robot. Change it name to what you want or the default “compass”
in the controller.py code:
from controller import Compass
Compass1=Compass("compass")
Compass1.enable(1)
print(Compass1.getValues())
could directly return a list with shape=[3], dtype=float64. The return shows the direction where the z-axis points to.
Add a Accelerometer as the children of the robot. Change it name to what you want or the default “accelerometer”
in the controller.py code:
from controller import Accelerometer
Accelerometer1=Accelerometer("accelerometer")
Accelerometer1.enable(1)
print(Accelerometer1.getValues())
could directly return a list with shape=[3], dtype=float64. The return shows the acceleration along each axis.
Add a GPS as the children of the robot. Change it name to what you want or the default “gps”
from controller import GPS
GPS1=GPS("gps")
GPS1.enable(1)
print(GPS1.getValues())