SlideShare a Scribd company logo
1 of 3
Download to read offline
Multithreading with Java leJOS
Juan Antonio Breña Moral Page 11 of 17 www.juanantonio.info
The original code which was coded without any Thread:
import lejos.nxt.*;
public class LineFollowerOld {
public static void main(String[] args) {
LightSensor ss = new LightSensor(SensorPort.S3);
ss.setFloodlight(true);
UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1);
while (us.getDistance()>25){
LCD.drawInt(ss.readValue(), 3, 9, 0);
LCD.refresh();
while (ss.readValue()< 45){
MotorPort.C.controlMotor(0, 3);
MotorPort.B.controlMotor(80, 1);
}
while(ss.readValue()>=45){
MotorPort.B.controlMotor(0, 3);
MotorPort.C.controlMotor(80, 1);
}
}
LCD.drawString("Object found!", 0,1);
Sound.twoBeeps();
Sound.twoBeeps();
}
}
Once you analyze the tasks:
1. Follow black lines
2. Discover obstacles in the route
It is very easy to design and develop an scalable solutions with Threads:
The main Program:
import lejos.nxt.*;
public class HarisRobot {
private static DataExchange DE;
private static LineFollower LFObj;
private static ObstacleDetector ODObj;
public static void main(String[] args){
DE = new DataExchange();
ODObj = new ObstacleDetector(DE);
LFObj = new LineFollower(DE);
ODObj.start();
LFObj.start();
while(!Button.ESCAPE.isPressed()){
//Empty
}
LCD.drawString("Finished",0, 7);
LCD.refresh();
Multithreading with Java leJOS
Juan Antonio Breña Moral Page 12 of 17 www.juanantonio.info
System.exit(0);
}
}
The thread used to follow a black line:
import lejos.nxt.*;
public class LineFollower extends Thread {
DataExchange DEObj;
private LightSensor ss;
private UltrasonicSensor us;
private final int colorPattern = 45;
public LineFollower(DataExchange DE){
DEObj = DE;
ss = new LightSensor(SensorPort.S3);
us = new UltrasonicSensor(SensorPort.S1);
ss.setFloodlight(true);
}
public void run(){
//Infinite Task
while(true){
if(DEObj.getCMD() == 1){
if(ss.readValue()< colorPattern){
MotorPort.C.controlMotor(0, 3);
MotorPort.B.controlMotor(80, 1);
}else{
MotorPort.B.controlMotor(0, 3);
MotorPort.C.controlMotor(80, 1);
}
LCD.drawInt(ss.readValue(), 3, 9, 0);
LCD.refresh();
}else{
//Stop
MotorPort.B.controlMotor(0, 3);
MotorPort.C.controlMotor(0, 3);
}
}
}
}
The Thread used to detect Obstacles
import lejos.nxt.*;
public class ObstacleDetector extends Thread{
private DataExchange DEObj;
private UltrasonicSensor us;
private final int securityDistance = 25;
public ObstacleDetector(DataExchange DE){
DEObj = DE;
us = new UltrasonicSensor(SensorPort.S1);
}
Multithreading with Java leJOS
Juan Antonio Breña Moral Page 13 of 17 www.juanantonio.info
public void run(){
while(true){
if(us.getDistance() > securityDistance){
DEObj.setCMD(1);
}else{
DEObj.setCMD(0);
//LCD Output
LCD.drawString("Object found!", 0,1);
LCD.refresh();
Sound.twoBeeps();
Sound.twoBeeps();
}
}
}
}
Finally the Class used to exchange data among Threads:
public class DataExchange {
//ObstacleDetector
private boolean obstacleDetected = false;
//Robot has the following commands: Follow Line, Stop
private int CMD = 1;
public DataExchange(){
}
/*
* Getters & Setters
*/
public void setObstacleDetected(boolean status){
obstacleDetected = status;
}
public boolean getObstacleDetected(){
return obstacleDetected;
}
public void setCMD(int command){
CMD = command;
}
public int getCMD(){
return CMD;
}
}
2.4.2.- Example2: Stereo US
In the project RC Car managed by NXT Brick,
http://www.juanantonio.info/jab_cms.php?id=228 exists 2 US Sensors which
detect obstacles.

More Related Content

What's hot

Senior design project code for PPG
Senior design project code for PPGSenior design project code for PPG
Senior design project code for PPGFrankDin1
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPMiller Lee
 
20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugs20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugsComputer Science Club
 
C++ amp on linux
C++ amp on linuxC++ amp on linux
C++ amp on linuxMiller Lee
 
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonLow-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonAMD Developer Central
 
20140531 serebryany lecture02_find_scary_cpp_bugs
20140531 serebryany lecture02_find_scary_cpp_bugs20140531 serebryany lecture02_find_scary_cpp_bugs
20140531 serebryany lecture02_find_scary_cpp_bugsComputer Science Club
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...Andrey Karpov
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321Teddy Hsiung
 
Games no Windows (FATEC 2015)
Games no Windows (FATEC 2015)Games no Windows (FATEC 2015)
Games no Windows (FATEC 2015)Fabrício Catae
 
Dependent voltage source (vsource)
Dependent voltage source (vsource)Dependent voltage source (vsource)
Dependent voltage source (vsource)Tsuyoshi Horigome
 
Vc4c development of opencl compiler for videocore4
Vc4c  development of opencl compiler for videocore4Vc4c  development of opencl compiler for videocore4
Vc4c development of opencl compiler for videocore4nomaddo
 

What's hot (19)

FINISHED_CODE
FINISHED_CODEFINISHED_CODE
FINISHED_CODE
 
Introduction to Data Oriented Design
Introduction to Data Oriented DesignIntroduction to Data Oriented Design
Introduction to Data Oriented Design
 
A Step Towards Data Orientation
A Step Towards Data OrientationA Step Towards Data Orientation
A Step Towards Data Orientation
 
Senior design project code for PPG
Senior design project code for PPGSenior design project code for PPG
Senior design project code for PPG
 
GPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMPGPU Programming on CPU - Using C++AMP
GPU Programming on CPU - Using C++AMP
 
Conflux:gpgpu for .net (en)
Conflux:gpgpu for .net (en)Conflux:gpgpu for .net (en)
Conflux:gpgpu for .net (en)
 
20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugs20140531 serebryany lecture01_fantastic_cpp_bugs
20140531 serebryany lecture01_fantastic_cpp_bugs
 
OpenGL L06-Performance
OpenGL L06-PerformanceOpenGL L06-Performance
OpenGL L06-Performance
 
C++ amp on linux
C++ amp on linuxC++ amp on linux
C++ amp on linux
 
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil PerssonLow-level Shader Optimization for Next-Gen and DX11 by Emil Persson
Low-level Shader Optimization for Next-Gen and DX11 by Emil Persson
 
20140531 serebryany lecture02_find_scary_cpp_bugs
20140531 serebryany lecture02_find_scary_cpp_bugs20140531 serebryany lecture02_find_scary_cpp_bugs
20140531 serebryany lecture02_find_scary_cpp_bugs
 
PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...PVS-Studio team experience: checking various open source projects, or mistake...
PVS-Studio team experience: checking various open source projects, or mistake...
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
 
Games no Windows (FATEC 2015)
Games no Windows (FATEC 2015)Games no Windows (FATEC 2015)
Games no Windows (FATEC 2015)
 
Dependent voltage source (vsource)
Dependent voltage source (vsource)Dependent voltage source (vsource)
Dependent voltage source (vsource)
 
Gpus graal
Gpus graalGpus graal
Gpus graal
 
AA-sort with SSE4.1
AA-sort with SSE4.1AA-sort with SSE4.1
AA-sort with SSE4.1
 
Vc4c development of opencl compiler for videocore4
Vc4c  development of opencl compiler for videocore4Vc4c  development of opencl compiler for videocore4
Vc4c development of opencl compiler for videocore4
 

Similar to Multithreading Java leJOS Line Following Robot

Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Platonov Sergey
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITEgor Bogatov
 
Anomalies in X-Ray Engine
Anomalies in X-Ray EngineAnomalies in X-Ray Engine
Anomalies in X-Ray EnginePVS-Studio
 
The IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IOT Academy
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...DroidConTLV
 
Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Svet Ivantchev
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisitionazhar557
 
Track c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -eveTrack c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -evechiportal
 
ISCA Final Presentaiton - Compilations
ISCA Final Presentaiton -  CompilationsISCA Final Presentaiton -  Compilations
ISCA Final Presentaiton - CompilationsHSA Foundation
 
codings related to avr micro controller
codings related to avr micro controllercodings related to avr micro controller
codings related to avr micro controllerSyed Ghufran Hassan
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidIntel® Software
 
Digital to analog -Sqaure waveform generator in VHDL
Digital to analog -Sqaure waveform generator in VHDLDigital to analog -Sqaure waveform generator in VHDL
Digital to analog -Sqaure waveform generator in VHDLOmkar Rane
 
Georgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software securityGeorgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software securityDefconRussia
 
Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Intel® Software
 
Programmation pic 16F877
Programmation pic 16F877Programmation pic 16F877
Programmation pic 16F877Mouna Souissi
 
Story of static code analyzer development
Story of static code analyzer developmentStory of static code analyzer development
Story of static code analyzer developmentAndrey Karpov
 
Grand centraldispatch
Grand centraldispatchGrand centraldispatch
Grand centraldispatchYuumi Yoshida
 
PVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio
 

Similar to Multithreading Java leJOS Line Following Robot (20)

Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.Евгений Крутько, Многопоточные вычисления, современный подход.
Евгений Крутько, Многопоточные вычисления, современный подход.
 
How to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJITHow to add an optimization for C# to RyuJIT
How to add an optimization for C# to RyuJIT
 
Anomalies in X-Ray Engine
Anomalies in X-Ray EngineAnomalies in X-Ray Engine
Anomalies in X-Ray Engine
 
The IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programmingThe IoT Academy IoT Training Arduino Part 3 programming
The IoT Academy IoT Training Arduino Part 3 programming
 
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
Tricks to Making a Realtime SurfaceView Actually Perform in Realtime - Maarte...
 
Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016Gaztea Tech Robotica 2016
Gaztea Tech Robotica 2016
 
Data Acquisition
Data AcquisitionData Acquisition
Data Acquisition
 
Track c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -eveTrack c-High speed transaction-based hw-sw coverification -eve
Track c-High speed transaction-based hw-sw coverification -eve
 
ISCA Final Presentaiton - Compilations
ISCA Final Presentaiton -  CompilationsISCA Final Presentaiton -  Compilations
ISCA Final Presentaiton - Compilations
 
codings related to avr micro controller
codings related to avr micro controllercodings related to avr micro controller
codings related to avr micro controller
 
Tools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in AndroidTools and Techniques for Understanding Threading Behavior in Android
Tools and Techniques for Understanding Threading Behavior in Android
 
Reporte vhdl9
Reporte vhdl9Reporte vhdl9
Reporte vhdl9
 
Direct analog
Direct analogDirect analog
Direct analog
 
Digital to analog -Sqaure waveform generator in VHDL
Digital to analog -Sqaure waveform generator in VHDLDigital to analog -Sqaure waveform generator in VHDL
Digital to analog -Sqaure waveform generator in VHDL
 
Georgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software securityGeorgy Nosenko - An introduction to the use SMT solvers for software security
Georgy Nosenko - An introduction to the use SMT solvers for software security
 
Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*Tools and Techniques for Understanding Threading Behavior in Android*
Tools and Techniques for Understanding Threading Behavior in Android*
 
Programmation pic 16F877
Programmation pic 16F877Programmation pic 16F877
Programmation pic 16F877
 
Story of static code analyzer development
Story of static code analyzer developmentStory of static code analyzer development
Story of static code analyzer development
 
Grand centraldispatch
Grand centraldispatchGrand centraldispatch
Grand centraldispatch
 
PVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around DisneyPVS-Studio for Linux Went on a Tour Around Disney
PVS-Studio for Linux Went on a Tour Around Disney
 

More from Mr. Chanuwan

High level programming of embedded hard real-time devices
High level programming of embedded hard real-time devicesHigh level programming of embedded hard real-time devices
High level programming of embedded hard real-time devicesMr. Chanuwan
 
Runtime performance evaluation of embedded software
Runtime performance evaluation of embedded softwareRuntime performance evaluation of embedded software
Runtime performance evaluation of embedded softwareMr. Chanuwan
 
Performance testing based on time complexity analysis for embedded software
Performance testing based on time complexity analysis for embedded softwarePerformance testing based on time complexity analysis for embedded software
Performance testing based on time complexity analysis for embedded softwareMr. Chanuwan
 
High-Performance Timing Simulation of Embedded Software
High-Performance Timing Simulation of Embedded SoftwareHigh-Performance Timing Simulation of Embedded Software
High-Performance Timing Simulation of Embedded SoftwareMr. Chanuwan
 
High performance operating system controlled memory compression
High performance operating system controlled memory compressionHigh performance operating system controlled memory compression
High performance operating system controlled memory compressionMr. Chanuwan
 
Performance and memory profiling for embedded system design
Performance and memory profiling for embedded system designPerformance and memory profiling for embedded system design
Performance and memory profiling for embedded system designMr. Chanuwan
 
Application scenarios in streaming oriented embedded-system design
Application scenarios in streaming oriented embedded-system designApplication scenarios in streaming oriented embedded-system design
Application scenarios in streaming oriented embedded-system designMr. Chanuwan
 
Software Architectural low energy
Software Architectural low energySoftware Architectural low energy
Software Architectural low energyMr. Chanuwan
 
Software performance simulation strategies for high-level embedded system design
Software performance simulation strategies for high-level embedded system designSoftware performance simulation strategies for high-level embedded system design
Software performance simulation strategies for high-level embedded system designMr. Chanuwan
 
Object and method exploration for embedded systems
Object and method exploration for embedded systemsObject and method exploration for embedded systems
Object and method exploration for embedded systemsMr. Chanuwan
 
A system for performance evaluation of embedded software
A system for performance evaluation of embedded softwareA system for performance evaluation of embedded software
A system for performance evaluation of embedded softwareMr. Chanuwan
 
Embedded architect a tool for early performance evaluation of embedded software
Embedded architect a tool for early performance evaluation of embedded softwareEmbedded architect a tool for early performance evaluation of embedded software
Embedded architect a tool for early performance evaluation of embedded softwareMr. Chanuwan
 
Performance prediction for software architectures
Performance prediction for software architecturesPerformance prediction for software architectures
Performance prediction for software architecturesMr. Chanuwan
 
Model-Based Performance Prediction in Software Development: A Survey
Model-Based Performance Prediction in Software Development: A SurveyModel-Based Performance Prediction in Software Development: A Survey
Model-Based Performance Prediction in Software Development: A SurveyMr. Chanuwan
 

More from Mr. Chanuwan (14)

High level programming of embedded hard real-time devices
High level programming of embedded hard real-time devicesHigh level programming of embedded hard real-time devices
High level programming of embedded hard real-time devices
 
Runtime performance evaluation of embedded software
Runtime performance evaluation of embedded softwareRuntime performance evaluation of embedded software
Runtime performance evaluation of embedded software
 
Performance testing based on time complexity analysis for embedded software
Performance testing based on time complexity analysis for embedded softwarePerformance testing based on time complexity analysis for embedded software
Performance testing based on time complexity analysis for embedded software
 
High-Performance Timing Simulation of Embedded Software
High-Performance Timing Simulation of Embedded SoftwareHigh-Performance Timing Simulation of Embedded Software
High-Performance Timing Simulation of Embedded Software
 
High performance operating system controlled memory compression
High performance operating system controlled memory compressionHigh performance operating system controlled memory compression
High performance operating system controlled memory compression
 
Performance and memory profiling for embedded system design
Performance and memory profiling for embedded system designPerformance and memory profiling for embedded system design
Performance and memory profiling for embedded system design
 
Application scenarios in streaming oriented embedded-system design
Application scenarios in streaming oriented embedded-system designApplication scenarios in streaming oriented embedded-system design
Application scenarios in streaming oriented embedded-system design
 
Software Architectural low energy
Software Architectural low energySoftware Architectural low energy
Software Architectural low energy
 
Software performance simulation strategies for high-level embedded system design
Software performance simulation strategies for high-level embedded system designSoftware performance simulation strategies for high-level embedded system design
Software performance simulation strategies for high-level embedded system design
 
Object and method exploration for embedded systems
Object and method exploration for embedded systemsObject and method exploration for embedded systems
Object and method exploration for embedded systems
 
A system for performance evaluation of embedded software
A system for performance evaluation of embedded softwareA system for performance evaluation of embedded software
A system for performance evaluation of embedded software
 
Embedded architect a tool for early performance evaluation of embedded software
Embedded architect a tool for early performance evaluation of embedded softwareEmbedded architect a tool for early performance evaluation of embedded software
Embedded architect a tool for early performance evaluation of embedded software
 
Performance prediction for software architectures
Performance prediction for software architecturesPerformance prediction for software architectures
Performance prediction for software architectures
 
Model-Based Performance Prediction in Software Development: A Survey
Model-Based Performance Prediction in Software Development: A SurveyModel-Based Performance Prediction in Software Development: A Survey
Model-Based Performance Prediction in Software Development: A Survey
 

Recently uploaded

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsAndrey Dotsenko
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfjimielynbastida
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 

Recently uploaded (20)

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Science&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdfScience&tech:THE INFORMATION AGE STS.pdf
Science&tech:THE INFORMATION AGE STS.pdf
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 

Multithreading Java leJOS Line Following Robot

  • 1. Multithreading with Java leJOS Juan Antonio Breña Moral Page 11 of 17 www.juanantonio.info The original code which was coded without any Thread: import lejos.nxt.*; public class LineFollowerOld { public static void main(String[] args) { LightSensor ss = new LightSensor(SensorPort.S3); ss.setFloodlight(true); UltrasonicSensor us = new UltrasonicSensor(SensorPort.S1); while (us.getDistance()>25){ LCD.drawInt(ss.readValue(), 3, 9, 0); LCD.refresh(); while (ss.readValue()< 45){ MotorPort.C.controlMotor(0, 3); MotorPort.B.controlMotor(80, 1); } while(ss.readValue()>=45){ MotorPort.B.controlMotor(0, 3); MotorPort.C.controlMotor(80, 1); } } LCD.drawString("Object found!", 0,1); Sound.twoBeeps(); Sound.twoBeeps(); } } Once you analyze the tasks: 1. Follow black lines 2. Discover obstacles in the route It is very easy to design and develop an scalable solutions with Threads: The main Program: import lejos.nxt.*; public class HarisRobot { private static DataExchange DE; private static LineFollower LFObj; private static ObstacleDetector ODObj; public static void main(String[] args){ DE = new DataExchange(); ODObj = new ObstacleDetector(DE); LFObj = new LineFollower(DE); ODObj.start(); LFObj.start(); while(!Button.ESCAPE.isPressed()){ //Empty } LCD.drawString("Finished",0, 7); LCD.refresh();
  • 2. Multithreading with Java leJOS Juan Antonio Breña Moral Page 12 of 17 www.juanantonio.info System.exit(0); } } The thread used to follow a black line: import lejos.nxt.*; public class LineFollower extends Thread { DataExchange DEObj; private LightSensor ss; private UltrasonicSensor us; private final int colorPattern = 45; public LineFollower(DataExchange DE){ DEObj = DE; ss = new LightSensor(SensorPort.S3); us = new UltrasonicSensor(SensorPort.S1); ss.setFloodlight(true); } public void run(){ //Infinite Task while(true){ if(DEObj.getCMD() == 1){ if(ss.readValue()< colorPattern){ MotorPort.C.controlMotor(0, 3); MotorPort.B.controlMotor(80, 1); }else{ MotorPort.B.controlMotor(0, 3); MotorPort.C.controlMotor(80, 1); } LCD.drawInt(ss.readValue(), 3, 9, 0); LCD.refresh(); }else{ //Stop MotorPort.B.controlMotor(0, 3); MotorPort.C.controlMotor(0, 3); } } } } The Thread used to detect Obstacles import lejos.nxt.*; public class ObstacleDetector extends Thread{ private DataExchange DEObj; private UltrasonicSensor us; private final int securityDistance = 25; public ObstacleDetector(DataExchange DE){ DEObj = DE; us = new UltrasonicSensor(SensorPort.S1); }
  • 3. Multithreading with Java leJOS Juan Antonio Breña Moral Page 13 of 17 www.juanantonio.info public void run(){ while(true){ if(us.getDistance() > securityDistance){ DEObj.setCMD(1); }else{ DEObj.setCMD(0); //LCD Output LCD.drawString("Object found!", 0,1); LCD.refresh(); Sound.twoBeeps(); Sound.twoBeeps(); } } } } Finally the Class used to exchange data among Threads: public class DataExchange { //ObstacleDetector private boolean obstacleDetected = false; //Robot has the following commands: Follow Line, Stop private int CMD = 1; public DataExchange(){ } /* * Getters & Setters */ public void setObstacleDetected(boolean status){ obstacleDetected = status; } public boolean getObstacleDetected(){ return obstacleDetected; } public void setCMD(int command){ CMD = command; } public int getCMD(){ return CMD; } } 2.4.2.- Example2: Stereo US In the project RC Car managed by NXT Brick, http://www.juanantonio.info/jab_cms.php?id=228 exists 2 US Sensors which detect obstacles.