# 13. ROS1 Sensor Development Course
## 13.1 Glowy Ultrasonic Sensor Control
* **Assembly**
### 13.1.2 Getting Ready
Prepare a glowy ultrasonic sensor and connect it to any IIC interface on Raspberry Pi expansion board with 4PIN wire as the picture shown.
:::{Note}
4PIN wire adopts anti-reverse plug in design, please don't insert violently.
:::
### 13.1.3 Glowy Ultrasonic Sensor Introduction
The ultrasonic ranging chip integrates ultrasonic transmitting circuit, receiving circuit, digital processing circuit, etc. This module adopts IIC communication interface through which the measured distance can be read.
Besides, on each probe, there is RGB light whose brightness can be adjusted and parameters of color channel can be modified to change the color.
### 13.1.4 Program Logic
Firstly, set the distance range. Then control RGB colored light to light on or get out though the change of the level. Lastly, control the color of light through changing the parameter of color channels.
The source code of this program is stored in [/home/ubuntu/puppypi/src/puppy_extend_demo/scripts/sonar_control_demo.py](../_static/source_code/ros1/puppy_extend_demo_scripts.zip)
### 13.1.5 Operation Steps
:::{Note}
Commands must be entered with strict attention to capitalization and spaces.
:::
(1) Turn on PuppyPi, and connect it to Raspberry Pi desktop via VNC.
(2) Click
at upper left corner to to open Terminator.
(3) Enter the following command and press Enter to run the game program.
```
rosrun puppy_extend_demo sonar_control_demo.py
```
(4) If need to close this program, we can press `Ctrl+C`. If it fails to close, please try again.
### 13.1.6 Program Outcome
After the program starts, place an obstacle in front of the glowy ultrasonic sensor. Then the measured distance will be printed on the terminal. And the RGB colored light will illuminate in corresponding color according to the distance.
**Distance ≤ 300mm**: red light on.
**300mm≤ Distance ≤ 500mm**: green light on.
**Distance ≥500mm**: blue light on.
### 13.1.7 Function Extension
* **Modify Distance Range**
The corresponding distance range of different light colors can be modified. For example, we set green light to turn on when "300550".
(1) Click
at upper left corner to open Terminator.
(2) Enter the following command and press Enter to open the program file.
```
rosed puppy_extend_demo sonar_control_demo.py
```
(3) Next, scroll down to the codes in the red frame. Press `i` key to enter the editing mode.
(4) Modify the values as the picture shown.
(5) After modification, press "Esc", input" :wq"and press Enter to save and exit editing.
* **Customize RGB Color**
Likewise, we can change the color of RGB light, for example yellow.
(1) According to step (1) and (2) in "Modify Distance Range", open program file.
(2) Locate the codes in the red frame. Press `i` key to enter editing mode.
(3) The color of RGB light can be changed through modifying RGB values. Modify `setRGB(1,(255,0,0))` as `setRGB(1,(255,255,0))`, and `setRGB(0,(255,0,0))` as `setRGB(0,(255,255,0))`.
RGB value refers to the content of Red, Green and Blue colors in one color. And all the colors can be constructed from the combination of the Red, Green and Blue colors.
(4) After modification, press **Esc**, input **wq** and press Enter to save the modified data.
## 13.2 Distance Ranging and Obstacle Avoidance
* **Assembly**
### 13.2.1 Getting Ready
Prepare a ultrasonic sensor and connect it to any IIC interface on Raspberry Pi expansion board with 4PIN wire as the picture shown.
:::{Note}
4PIN wire adopts anti-reverse plug in design, please don't insert violently.
:::
### 13.2.2 Ultrasonic Sensor Introduction
The ultrasonic ranging chip integrates ultrasonic transmitting circuit, receiving circuit, digital processing circuit, etc. This module adopts IIC communication interface through which the measured distance can be read.
Besides, on each probe, there is RGB light whose brightness can be adjusted and parameters of color channel can be modified to change the color.
### 13.2.3 Program Logic
Firstly, set the distance range. Then judge whether there is obstacle ahead through the change of level. Next, program PuppyPi to execute different actions based on the previous judgement.
The source code of this program is stored in [/home/ubuntu/puppypi/src/puppy_extend_demo/scripts/sonar_avoidance.py](../_static/source_code/ros1/puppy_extend_demo_scripts.zip)
### 13.2.4 Operation Steps
(1) Turn on PuppyPi and connect it to RaspberryPi desktop via VNC.
(2) Click
at upper left corner to enter Terminator terminal.
(3) Enter the following command and press Enter to run the game program.
```
rosrun puppy_extend_demo sonar_avoidance.py
```
(4) If need to close this program, we can press `Ctrl+C`. If it fails to close, please try again.
### 13.2.5 Program Outcome
After the program starts, place an obstacle in front of the glowy ultrasonic sensor. Then the measured distance will be printed on the terminal. When distance is greater than 300mm, RGB light will emit blue light and PuppyPi will keep moving forward. When the distance is less than or equal to 300mm, RGB light will emit red light and PuppyPi will keep turning left.
## 13.3 Touch Sensor Installation and Sensing
* **Assembly**
### 13.3.1 Getting Ready
Prepare a touch sensor and connect it to **5V GND IO22 IO24** interface on Raspberry Pi expansion board with 4PIN wire as the picture shown.
:::{Note}
4PIN wire adopts anti-reverse plug in design, please don't insert violently.
:::
### 13.3.2 Touch Sensor Introduction
Based on capacitive sensing, Hiwonder touch sensor can sense the touch from human body or metal. In addition, the contact between plastic, paper and other materials of certain thickness can also be sensed. Its sensitivity is related to contact area and material thickness.
After powering, signal terminals OUT will output high-level signal when sensor does not sense the touch signal. When the touch signal is sensed, signal terminal OUT will output low-level signal. It is applicable in switch control, such as light switch and doorbell touch buttons. The holes on the module are compatible with Lego for more creative DIY designs.
### 13.3.3 Program Logic
When sensing the touch, touch sensor will output high level, otherwise out low level. We can judge the status of the sensor through the level change of I/O interface.
The source code of this program is stored in [/home/ubuntu/puppypi/src/puppy_extend_demo/scripts/touch_detect_demo.py](../_static/source_code/ros1/puppy_extend_demo_scripts.zip)
### 13.3.4 Operation Steps
(1) Turn on PuppyPi and connect it to Raspberry Pi desktop via VNC.
(2) Click
at upper left corner to enter Terminator terminal.
(3) Enter the following command and press Enter to run the game program.
```
rosrun puppy_extend_demo touch_detect_demo.py
```
(4) If need to close this program, we can press **Ctrl+C**. If it fails to close, please try again.
### 13.3.5 Program Outcome
After the program starts, buzzer will beep once when sensing the touch on metal plate.
## 13.4 Dot Matrix Display
* **Assembly**
### 13.4.1 Getting Ready
Prepare a dot matrix display and connect it to **5V GND IO7 IO8** interface on Raspberry Pi expansion board with 4PIN wire as the picture shown.
:::{Note}
4PIN wire adopts anti-reverse plug in design. Please do not insert it to the interface violently.
:::
### 13.4.2 Dot Matrix Display Introduction
Dot matrix display module composed of two 8x8 LED dot matrix screens, which can be controlled through driving the control chip. It features high brightness, no-flicker display, convenient wiring, etc. And it can display various content, including number, text, pattern, etc.
### 13.4.3 Program Logic
In this program, a set of hexadecimal data is used to control the dot matrix screen to display the content. A set of hexadecimal data consists of 16 data each of which controls a column of LED.
It is simple to set the data. For example, control the dot matrix display to display **Hello**.
The parameter in brackets of 32nd and 33rd line are the array for setting the displayed pattern. The converted binary number of its first element `0x7f` is 01111111 which represents the light status, that is off on on on on on on on.
As the same, the rest 15 elements are also for controlling the LED lights to display "Hello".
:::{Note}
You can find "Instructions for Using the Font Software" in the same directory of this section, where you can quickly obtain control arrays using the font software.
:::
The source code of this program locates in [/home/ubuntu/puppypi/src/puppy_extend_demo/scripts/lattice_display_demo.py](../_static/source_code/ros1/puppy_extend_demo_scripts.zip)
### 13.4.4 Operation Steps
(1) Turn on PuppyPi, and connect it to Raspberry Pi desktop via VNC.
(2) Click
to enter Terminator terminal.
(3) Enter the following command and press Enter to start the game.
```
rosrun puppy_extend_demo lattice_display_demo.py
```
(4) If want to close this program, press **Ctrl+C**.
### 13.4.5 Project Outcome
After the program runs, the dot matrix display will display **Hello** for 5s. Then, the program will automatically exit and the dot matrix display will be closed.
### 13.4.6 Function Extension
The current displayed content is **Hello**. If you need to modify the content, like **Love**, please follow the below steps to operate.
Before modification, we need to obtain the address of the character on the dot matrix display through the Internet.
(1) Double-click to open the CharacterMatrix software in this directory.
(2) First, click on "**New Image**", then in the pop-up settings box, set the size parameters for the dot matrix module. Here, set it to "**16*8**". After setting, click "**OK**".
(3) Then click "**Simulated Animation**" and "**Enlarge Grid**" in order to enlarge the dot matrix simulation area on the right side.
(4) Then, use the mouse to click and draw the display content in the right area.
(5) After finishing the drawing, click "**Modeling Method**" and then "**51 Format**" in sequence to obtain the address symbol (please remember the address symbol for the subsequent steps).
(6) Next, input command and press Enter to open the game program file.
```
rosed puppy_extend_demo lattice_display_demo.py
```
(7) Find the following code in the interface.
(8) Press `i` key on the keyboard to enter the editing mode.
(9) Replace the default address symbol in the program with the address symbol obtained from the font software, as shown in the following figure:
(10) After modification, press **Esc** and enter **wq** and the press Enter to save the modified content
## 13.5 Touch Detection
* **Assembly**
### 13.5.1 Preparation
Prepare a touch sensor and connect it to **5V GND IO22 IO24** interface on Raspberry Pi expansion board with 4PIN wire as the picture shown.
:::{Note}
4PIN wire adopts anti-reverse plug in design, please don't insert violently.
:::
### 13.5.2 Touch Sensor Introduction
Based on capacitive sensing, Hiwonder touch sensor can sense the touch from human body or metal. In addition, the contact between plastic, paper and other materials of certain thickness can also be sensed. Its sensitivity is related to contact area and material thickness.
After powering, signal terminals OUT will output high-level signal when sensor does not sense the touch signal. When the touch signal is sensed, signal terminal OUT will output low-level signal. It is applicable in switch control, such as light switch and doorbell touch buttons. The holes on the module are compatible with Lego for more creative DIY designs.
### 13.5.3 Program Logic
When sensing the touch, touch sensor will output high level, otherwise out low level. We can judge the status of the sensor through the level change of I/O interface.
The source code of this program is stored in [/home/ubuntu/puppypi/src/puppy_extend_demo/scripts/touch_control_demo.py](../_static/source_code/ros1/puppy_extend_demo_scripts.zip)
**GPIO.input** function is used to acquire the value given by touch sensor so as to judge the status of the sensor.
### 13.5.4 Operation Steps
(1) Turn on PuppyPi and connect it to Raspberry Pi desktop via VNC.
(2) Click
at upper left corner to enter Terminator terminal.
(3) Enter the following command and press Enter to run the game program.
```
rosrun puppy_extend_demo touch_control_demo.py
```
(4) If need to close this program, we can press **Ctrl+C**. If it fails to close, please try again.
### 13.5.5 Program Outcome
After the program starts, PuppyPi will keep standing. When we first touch the metal plate of touch sensor, PuppyPi will squat when sensing the touch. When we touch the metal plate twice, PuppyPi will shake.
