HomeTurtlegraficsGPanelRobotics WebTigerPython
 Python - Online
touchsensor
Deutsch   English   

8. TOUCH SENSORS

 

 

YOU LEARN HERE...

 

how you can use touch sensors to detect obstacles and react accordingly.

 

 

HOW DOES A TOUCH SENSOR WORK

 
The touch sensor is designed like a switch that makes contact when the small metal pin is pressed. The touch sensor recognizes two states: pressed (1) and not pressed (0). The Callibot has two touch sensors.  
 
 

To detect contact with objects, it is practical to attach a bumper to the front of the Callibot. If the robot touches an object head-on, both sensors are pressed. If it touches the side, only the left or right sensor is pressed.
You can order the bumper from knotech.de.

 


If you have a 3D printer at your disposal, you can make a bumper like this yourself. Pascal Lütscher (Pädagogische Hochschule Graubünden) has created 3D printing templates and made them available to us.

https://www.tinkercad.com/things/0bfM0UhJQOk (bumper)
https://www.tinkercad.com/things/bqsSISDhCit (screws to bumper)

 

 

EXAMPLES

 

Example 1: Registering and avoiding an obstacle

Registering and avoiding an obstacle
The robot is to move in a room with four walls. If it touches a wall with the touch sensor, it moves back a short distance, turns 90° to the left and continues in the new direction.

If the touch sensor touches a wall head-on, both sensors are pressed and the tsValue() function returns 1. The sensor values are queried every 100 ms in a repeat loop.

 

Program:

from callibot import *

forward()
repeat:
    ts = tsValue() 
    if ts == 1:
        backward()
        delay(1500)
        left()
        delay(550)
        forward()
    delay(100)
► Copy to clipboard

 

Example 2: Channel robot
A robot with two touch sensors is to find its way in the channel. You can control the robot more efficiently with two touch sensors. Depending on whether it touches an obstacle with the left or right sensor, it first moves back briefly and turns by a small angle in the opposite direction. You can check whether the left or right sensor has touched the obstacle using the tsLLeftValue() or tsRightValue(). functions. These return 1 if the sensor was touched, otherwise 0.
Build an obstacle course and test the following program.

 




Program:

from callibot import *

setSpeed(30)
forward()
repeat:
    tsL = tsLeftValue()
    tsR = tsRightValue()
    if tsL == 1:
        backward()
        delay(250)
        right()
        delay(200)
        forward() 
    elif tsR == 1:        
        backward()
        delay(250)
        left()
        delay(200)
        forward()
    delay(100)
► Copy to clipboard


 

REMEMBER...

 

The touch sensors can only return two values: 1 if the sensor was pressed, otherwise 0. There are three functions available to you for querying the sensor values:

  • tsValue() is used when the bumper is touched head-on
  • tsLeftValue() is used to intercept the touch of the left sensor
  • tsRightValue() returns 1 if the right sensor was touched, otherwise 0

 

 

TO SOLVE BY YOURSELF

 

 


1.


A robot with one or two touch sensors moves in a cage with one output. It starts in the middle and should find the exit as quickly as possible.

 

 
 



2.

The robot should move along a rectangular area, which is bounded on all sides, as if it were mowing it like a lawnmower. If it encounters a boundary, it first moves back a little, turns 90 degrees, moves a short distance parallel to the wall, turns 90 degrees again and moves forward again.

Note that the lawn mower must always turn once to the left and once to the right. There is a simple trick to program it this way:

 

You select variable n, which is 0 at the beginning, is set to 1 after a left turn and is set to 0 again for a right turn. You check at each turn whether n is 0 or 1 and make the corresponding turn. To structure the program better, you can define the left turn as the function turnLeft() and the right turn as the function turnRight().


3.

Channel robot.
A robot with two touch sensors should travel through the entire pipe and return to the starting point.

 

 

 

 

 
 
4.

Your robot should travel through the adjacent labyrinth using its touch sensor and the buttons button_a and button_b. It starts at the bottom right and moves forward. If it touches a wall with the touch sensor, it moves backwards a short distance, stops and waits until you have pressed the left or right button. It then turns at right angles in this direction and moves forward..