Skip to content

Commit 21f418b

Browse files
Merge pull request #394 from oss-slu/384-ir-obstacle-avoidance-module-support
Yaml and helper class
2 parents 389ad94 + ff40d49 commit 21f418b

File tree

9 files changed

+518
-1
lines changed

9 files changed

+518
-1
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.opensourcewithslu.components.controllers;
2+
3+
import java.awt.Event;
4+
5+
import com.opensourcewithslu.inputdevices.IRObstacleAvoidanceHelper;
6+
import com.pi4j.context.Context;
7+
import com.pi4j.io.gpio.digital.DigitalInput;
8+
import com.pi4j.io.gpio.digital.DigitalState;
9+
10+
import io.micronaut.http.annotation.Controller;
11+
import io.micronaut.http.annotation.Get;
12+
import jakarta.inject.Inject;
13+
import jakarta.inject.Named;
14+
//tag::ex[]
15+
@Controller("/ir-obstacle")
16+
public class IRObstacleAvoidanceController {
17+
18+
private final IRObstacleAvoidanceHelper irObstacleAvoidanceHelper;
19+
20+
// Inject the IR obstacle avoidance digital input pin
21+
@Inject
22+
public IRObstacleAvoidanceController(@Named("ir-obstacle-input") DigitalInput digitalInput) {
23+
this.irObstacleAvoidanceHelper = new IRObstacleAvoidanceHelper(digitalInput);
24+
}
25+
26+
// Endpoint to check if an obstacle is currently detected
27+
@Get("/status")
28+
public boolean isObstacleDetected() {
29+
return irObstacleAvoidanceHelper.isObstacleDetected();
30+
}
31+
32+
// Endpoint to get the current state (HIGH or LOW)
33+
@Get("/state")
34+
public DigitalState getState() {
35+
return irObstacleAvoidanceHelper.getState();
36+
}
37+
38+
// Endpoint to get human-readable status message
39+
@Get("/message")
40+
public String getStatusMessage() {
41+
return irObstacleAvoidanceHelper.getStatusMessage();
42+
}
43+
44+
// Endpoint to wait for obstacle detection with timeout
45+
@Get("/wait/{timeoutMillis}")
46+
public boolean waitForObstacle(long timeoutMillis) throws InterruptedException {
47+
return irObstacleAvoidanceHelper.waitForObstacle(timeoutMillis);
48+
}
49+
// initializing endpoint
50+
@Get("/init")
51+
public void enable() {
52+
irObstacleAvoidanceHelper.addEventListener (event ->{
53+
if (irObstacleAvoidanceHelper.isObstacleDetected()){
54+
System.out.println("Obstacle Detected");
55+
} else {
56+
System.out.println("No Obstacle");
57+
}
58+
});
59+
}
60+
}
61+
//end::ex[]

components/src/main/resources/application.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,12 @@ pi4j:
150150
pull: PULL_UP
151151
debounce: 200
152152
provider: pigpio-digital-input
153+
ir-obstacle-input:
154+
name: IR Obstacle Avoidance Sensor
155+
address: 17
156+
pull: PULL_UP
157+
debounce: 10000
158+
provider: pigpio-digital-input
153159
# end::digitalInput[]
154160

155161
# tag::digitalOutput[]

pi4micronaut-utils/src/docs/asciidoc/Introduction/supportedHardware.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ We plan on offering support for many more hardware components in the future. If
2121
* Ultrasonic Sensor
2222
* Servo Motor
2323
* Tilt Switch
24-
* Micro Switch
24+
* Micro Switch
25+
* IR Obstacle Avoidance Sensor
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
:imagesdir: img/
2+
3+
ifndef::rootpath[]
4+
:rootpath: ../../
5+
endif::rootpath[]
6+
7+
ifdef::rootpath[]
8+
:imagesdir: {rootpath}{imagesdir}
9+
endif::rootpath[]
10+
11+
==== IR Obstacle Avoidance Sensor
12+
[.text-right]
13+
https://github.com/oss-slu/Pi4Micronaut/edit/develop/pi4micronaut-utils/src/docs/asciidoc/components/inputComponents/irObstacleAvoidanceSensor.adoc[Improve this doc]
14+
15+
===== Overview
16+
This section provides details for the IR Obstacle Avoidance Sensor, including its components, assembly instructions, and functionality.
17+
18+
The IR (Infrared) Obstacle Avoidance Module is a proximity sensor that detects objects and obstacles within a short distance (2-30cm) using infrared reflection. It is commonly used in robotics for collision avoidance, object detection, and boundary sensing. The module features adjustable sensitivity via a potentiometer, digital output for easy integration, fast response time, low power consumption, and operates on 3.3V to 5V power supplies.
19+
20+
===== Components
21+
* 1 x Raspberry Pi 4
22+
* 1 x IR Obstacle Avoidance Module
23+
* 5 x Male-to-Male Jumper Wires
24+
* 1 x Breadboard
25+
* Power supply for Raspberry Pi
26+
27+
===== Assembly Instructions
28+
* Place the IR Obstacle Avoidance Module onto the breadboard.
29+
* Connect the **VCC** pin of the IR module to **3.3V** on the Raspberry Pi.
30+
* Connect the **GND** pin of the IR module to **Ground** on the Raspberry Pi.
31+
* Connect the **OUT** (or **DO**) pin of the IR module to **GPIO 17** on the Raspberry Pi.
32+
* Use the remaining two jumper wires to secure the module to the breadboard, following the provided circuit diagram.
33+
* Verify connections to ensure no loose wires or short circuits.
34+
* Adjust the potentiometer to set detection sensitivity:
35+
- Turn **clockwise** to increase sensitivity.
36+
- Turn **counter-clockwise** to decrease sensitivity.
37+
38+
> **Tip**: Use 3.3V for compatibility with Raspberry Pi GPIO. Ensure the module's IR LEDs face forward.
39+
40+
===== Circuit Diagram
41+
image::irObstacleAvoidance-CD.png[]
42+
43+
===== Schematic Diagram
44+
image::irObstacleAvoidance-SD.png[]
45+
46+
===== Functionality
47+
The IR Obstacle Avoidance Module uses active infrared light reflection to detect obstacles. It emits infrared light via an onboard LED, which reflects off nearby objects and is detected by the IR receiver. The onboard comparator circuit processes the signal and outputs a digital signal (LOW when an obstacle is detected, HIGH when the path is clear). The detection range (2-30cm) is adjustable via the potentiometer, making it suitable for real-time robotics applications.
48+
49+
===== Testing
50+
Use the following commands to test the component:
51+
52+
[source, bash]
53+
----
54+
$curl http://localhost:8080/ir-obstacle/status
55+
----
56+
* `/status` - Returns `true` if an obstacle is detected, `false` if the path is clear.
57+
58+
[source, bash]
59+
----
60+
$curl http://localhost:8080/ir-obstacle/state
61+
----
62+
* `/state` - Returns the raw digital state (`HIGH` or `LOW`).
63+
64+
[source, bash]
65+
----
66+
$curl http://localhost:8080/ir-obstacle/message
67+
----
68+
* `/message` - Returns a human-readable status (`"Obstacle detected"` or `"No obstacle"`).
69+
70+
[source, bash]
71+
----
72+
$curl http://localhost:8080/ir-obstacle/wait/{timeoutMillis}
73+
----
74+
* `/wait/{timeoutMillis}` - Waits for obstacle detection within the specified timeout (in milliseconds) and returns `true` if detected, `false` otherwise.
75+
76+
[source, bash]
77+
----
78+
$curl http://localhost:8080/ir-obstacle/init
79+
----
80+
* `/init` - Initializes the sensor and adds an event listener to print `"Obstacle Detected"` or `"No Obstacle"` to the console when the sensor state changes.
81+
82+
===== Troubleshooting
83+
**Sensor Always Reports Obstacle Detected**:
84+
* Adjust potentiometer counter-clockwise to decrease sensitivity.
85+
* Ensure the module is not facing a reflective surface.
86+
* Verify VCC is connected to 3.3V.
87+
* Test with a white object to confirm detection.
88+
* Check GPIO state with `gpio readall`.
89+
90+
**Sensor Never Detects Obstacles**:
91+
* Adjust potentiometer clockwise to increase sensitivity.
92+
* Verify OUT pin is connected to GPIO 17.
93+
* Confirm `application.yml` has the correct GPIO address.
94+
* Test with a reflective object (e.g., white or shiny surface).
95+
* Measure OUT pin voltage with a multimeter (should switch between 0V and 3.3V).
96+
97+
**Inconsistent Detection**:
98+
* Increase debounce time in `application.yml` to 20000 (20ms).
99+
* Secure all jumper wires.
100+
* Shield from sunlight or other IR sources.
101+
* Mount the module rigidly to prevent vibration.
102+
* Add a 100µF capacitor across VCC and GND near the module.
103+
104+
**Module Won't Power On**:
105+
* Verify 3.3V at VCC with a multimeter.
106+
* Check GND connections.
107+
* Ensure `application.yml` is correctly configured.
108+
* Test GPIO manually with `gpio mode 17 in` and `gpio read 17`.
109+
110+
**Application Crashes**:
111+
* Verify `application.yml` syntax and `ir-obstacle-input` key.
112+
* Ensure `@Named("ir-obstacle-input")` matches the configuration.
113+
* Rebuild with `./gradlew clean build`.
114+
* Check logs for configuration errors.
115+
116+
===== YAML Configuration
117+
The IR Obstacle Avoidance Sensor configuration as it appears in the YAML file:
118+
119+
[source, yaml]
120+
----
121+
pi4j:
122+
digital-input:
123+
ir-obstacle-input:
124+
name: IR Obstacle Avoidance Sensor
125+
address: 17
126+
pull: PULL_UP
127+
debounce: 10000
128+
provider: pigpio-digital-input
129+
----
130+
131+
===== Constructor and Methods
132+
To see the constructor and methods of the `IRObstacleAvoidance` class, refer to the JavaDoc: link:https://oss-slu.github.io/Pi4Micronaut/javadoc/com/opensourcewithslu/inputdevices/IRObstacleAvoidance.html[IRObstacleAvoidance JavaDoc]
133+
134+
===== Example Controller
135+
This controller demonstrates the usage of the IR Obstacle Avoidance Sensor:
136+
137+
[source, java]
138+
----
139+
include::../../../../../../components/src/main/java/com/opensourcewithslu/components/controllers/IRObstacleAvoidanceController.java[tag=ex]
140+
----
98.1 KB
Loading
7.18 KB
Loading

pi4micronaut-utils/src/docs/asciidoc/index.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ include::components/outputComponents.adoc[]
6565
include::components/outputComponents/fourDigitSevenSegment.adoc[]
6666
include::components/outputComponents/activebuzzer.adoc[]
6767
include::components/outputComponents/fan.adoc[]
68+
include::components/inputComponents/IRObstacleAvoidance.adoc[]
6869
include::components/outputComponents/lcd1602.adoc[]
6970
include::components/outputComponents/led.adoc[]
7071
include::components/outputComponents/motor.adoc[]

0 commit comments

Comments
 (0)