2018
February Tuesday 20

Bookmark and Share

Related Articles...

MILWAUKEE WI Arduino Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Arduino Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Raspberry Pi Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Raspberry Pi Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Internet of Things (IoT) Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Make Space Acquired!

Full Advanced Fabrication Facility, machine Shop, Welding, Electronics Lab Equipment Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Machine Repair, & Installation  - Tier 1 Business Support

Machine Technician Mechanic 20+ Years  Personal Experienced In Industrial Factory Machine Servicing, Rebuilding, Installation And Repair - Plus A 35+ Years Experienced Computer Engineer Service Technician Team Ready For Your Outsourced Market Advantage Needs!

Hartung Industrial - Member - CGI Science & Technology Group

Email :

COASMailBox @ gmail.com

2018 JAN REVIEW

Arduino Open Source Community member - Tier 1 Full Stack Developer

Arduino Parellel Processing

MODERN COMMERCIAL PROGRAMMING

2018 JAN | by Gene Casanova

Senior Systems Engineer


History

Intel "Hyperthreading"

'Hyperthreading' is a technology the Intel company came up to make a single-core processor, simulate a two core processor; or a two core processor simulate 4 cores, etc.

Microcontrollers and CPUs do work in "cycles of logic".  How fast they do them (how many cycles in a second) is the "clock rate".  The general public thinking follows this concept: the more cycles per second, the better.

The Arduino UNO is a development systemboard for a Atmel ATMega328P microntroller processor, running at 16Mhz.  This Mcrocontroller processor is capable of running at 20Mhz.

16Mhz stands for 16,000,000 cycles per second; 16 million 'states' (a unit of work) per second.

The limit to getting work done by a computer is no longer limited to the CPU 16Mhz speed limit.  Multicore, multithreading, and protothreading are taking computing technology to the next level.

"Multicore" processors, and "multithreading" processors

Multithreaded applications are two separate processes, being processed in parallel on different cores of a CPU in a computer system.  These processes interact to get work done together, but do not necessarily split the work evenly.  There is typically a main process / "thread" functioning as a manager of the other threads, and then one or more 'worker threads' it manages, which each might do specific tasks.

"Protothreading" is not multithreading.  A one core microcontroller, cannot process multithreading.  With one core, work must be managed while trying to do multiple things at the same time.  This is where "protothreading" technology is used.


Protothreading

Protothreading is simillar to hyperthreading.  Hyperthreading emulates a second core and literally divides the work one core is doing, by simulating two virtual cores.  The virtual cores are built into the real core; sharing the same resource space.

The microcontroller of an Arduino is not made with hyperthreading.

"Protothreading" is a programming technique, where work is accomplished by 'loops' with the flow of a processing program.

There are different implementations of the protothreading concept.

Some microcontroller models, support a real-time operating system (RTOS), providing more hyperthreading-like multitasking abilities which can help mitigate problems caused by "blocking" tasks.


Formulate Protothreading

Begin by thinking about and listing the tasks to be performed by the total process and microcontroller.

As an example, I will follow a common subject on the Internet, driving a LCD screen.

Tasks:

A) Fade the backlight of a LCD display panel; fade-in and fade-out to provide a "pulsing" effect.

B) While counting up a number on a much slower interval,

C) And rotating message strings at a slower interval.

Next, give a priority level to each process; from the least-blocking of other processes to the most-blocking of other processes.  This means a process like reading data or have long delays preventing the flow of the program to progress until the process finishes.  Processes which run frequently, and do not take a long time to complete, are the least-blocking processes.

The very least blocking processes is what should be used as the primary "thread".

In the example above, the 'a' task is a regular and very fast interval, perpetual with no delays in between running other than getting the task done, and the task is very fast.  This is the perfect manager "thread".

Use this 'thread' (and any loops within it) to check if the other threads need to do any work.

Example Source Code

The example source code uses the millis() function to perform 3 repeating tasks with separate time intervals, all at the same time on one Arduino UNO system.

The resulting program will (a) fade the LCD backlight in and out, while (b) incrementing a number, and (c) rotating between a few message strings.


Protothreading Foundation

The following source code builds the foundation for developing a protothreading process with multiple independant processes all running on one Arduino UNO.

/* 2018
 * Arduino UNO Prototyping Example
 * 
 * EXPERIMENTAL
 * 
 * By Gene Casanova
 * Professional computer-programming concepts.
*/
#include <elapsedMillis.h>
int led = 13;  
/* An 'Arduino UNO' full size development board comes with * a surface-mounted micro LED device, connected to * Digital I/O pin 13. * The 'Arduino IDE 1.5' and above version application, enables * using a pre-defined variable "LED_BUILTIN" to represent the built-in * LED and pin 13.
// Define a timer variable: elapsedMillis timer0;
// Define a time ineterval in milliseconds: #define interval 1000 // Set I/O pin state to 'LOW': boolean led = LOW; void setup() {
// Set Digital I/O Pin to OUTPUT mode:
pinMode(led, OUTPUT);
timer0 = 0; // Reset timer to zero; ensuring timer is accurate at start of loop(); // independent of the time consumed by the startup() function.
} void loop() {
// If Interval time (interval0) is greater than current run-time: if (timer0 > interval) {
timer0 -= interval; // Reset timer0. Enables varying the timing interval.
// Read LED Pin State: int ledPin = digitalRead(led);
// Write The Opposite State:
digitalWrite(led, !ledPin);
}
}

The above example demonstrates parellel processing based on timing intervals.  The process is enabled to conactntly run without processing delays introduced by using the fucntion delay().

During each pass of the loop() above, an interval is compared to a free running timer (timer0).
When the timer (timer0) exceeds the value of the interval, the desired process begins (in this example change the state of the I/O pin 13) and the timer (timer0) is reset.


Example Alternative

The soure code above rewritten without using the addtional library.

This code uses the millis() function; it returns the number of milliseconds since the microprocessesor started running the program.

/* 2018
 * Arduino UNO Prototyping Example
 * * EXPERIMENTAL *
 * * By Gene Casanova
	* * COPYRIGHT 2018
 * * Professional Computer-Programming Methods and Concepts.
*/
int led = 13;
unsigned long timer;				// Timer
unsigned long INTERVAL = 1000;	// Interval (time passed).


void setup() {
pinMode(led, OUTPUT); // Put I/O pin in OUTPUT mode.
timer = millis(); // Start timer.
} void loop() { // (main thread.)
if ((millis()-timer) > INTERVAL) { // When timer greater than interval, run the following process:
// Interval
timer += INTERVAL; // Reset timer by moving it forward to next interval.
// Read LED Pin State: int ledPin = digitalRead(led);
// Write Opposite State Read:
digitalWrite(led, !ledPin);
}
}

Alternative

/* 2018
 * Arduino UNO Prototyping Example
 * EXPERIMENTAL
 * By Gene Casanova
	* COPYRIGHT 2018
 * Professional computer-programming concepts.
 * millis() = Returns number of milliseconds since Arduino microprocessor began executing program.
 * This number will overflow/reset to zero, after approximately 50 days.
 * Introducing the concept of a "State Machine".
 * 	The current state of the I/O pin and the last time state changed is saved.
 *	On each pass through the loop(), the process checks the current millis() system time, and if there is a time interval
	*	to trigger a state machine to begin processing a task.  The program below prsents on state machine, changing the state
	*	of a LED from LOW to HIGH, And HIGH to LOW.
*/
const int led = 13;
unsigned long timer;				// Timer
const unsigned long INTERVAL = 1000;	// Interval value

int ledState = LOW;		//	Start LED off.


void setup() {
pinMode(led, OUTPUT); // Put I/O pin in OUTPUT mode.
} void loop() { unsigned long currentMillis = millis(); // Creat variable, put current time in variable (reset timing process). // IF the difference between current-time and last-time executed is greater than interval-time). // then execute task; change state of I/O pin. if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // Save last time LED changed. if (ledState == LOW) {
ledState = HIGH;
}else{
ledState = LOW;
} // Write state to I/O pin:
    digitalWrite(ledPin, ledState);
} }

Advanced State Machine

With an advanced state machine, many things can be happening and managed at the sma etime, in very fine detail.

The following program is a state machine with fine control over an LED.

/* 2018
 * Arduino UNO Prototyping Example
 * EXPERIMENTAL
 * By Gene Casanova
	* COPYRIGHT 2018
 * Professional computer-programming concepts.
*/
const int ledPin = 13;		// This can be set to any one of the Digital I/O pins.
long TimeOn	= 250;		//	Fine LED Control: milliseconds LED Is On.
long TimeOff = 750; // Fine LED Control: milliseconds LED Is Off. unsigned long previousMillis = 0; int ledState = LOW; // Start LED off. void setup() {
pinMode(ledPin, OUTPUT); // Put 'ledPin' I/O Pin in OUTPUT mode.
} void loop() { unsigned long currentMillis = millis(); // Creat variable currentMillis, put current time in variable (reset timing process). // IF the difference between current-time and last-time executed is greater than interval-time). // then execute task; change state of I/O pin. if ( (ledState == HIGH) && (currentMillis - previousMillis >= TimeOn) ) { ledState = LOW; // Set state to LOW. previousMillis = currentMillis; // Save current time.
digitalWrite(ledPin, ledState); // Write to I/O pin; Turns LED OFF.
} else if ((ledState == LOW) && (currentMillis - previousMillis >= TimeOff) ) { ledState = HIGH; // Set state to HIGH.
previousMillis = currentMillis; // Save current time.
digitalWrite(ledPin, ledState); // Write to I/O pin; Turns LED ON.
} }

The above soure code builds a state machine, with variables to track the state of an attached LED (ON or OFF);  and variables to track when the last state change happened.   This is the "State" part of this "State Machine".

The source code has logic for changing the state of an I/O pin.  This part of the source code is the "Machine" part of this State Machine.   Every time through the loop, 'run the machine'; the machine updates the state.


Multiprocesing, Protothreading

Create another state machine to handle another task, to control a second LED completely independantly of the first LED.

Using two separate state machines, enable doing two seperate indenpendant tasks.

/* 2018
 * Arduino UNO Prototyping Example
 * EXPERIMENTAL
 * By Gene Casanova
	* COPYRIGHT 2018
 * Professional computer-programming concepts.
*/
const int ledPin1 = 13;		// This can be set to any one of the Digital I/O pins.
long TimeOn1	= 250;		//	Fine LED Control: milliseconds LED Is On.
long TimeOff1 = 750; // Fine LED Control: milliseconds LED Is Off. unsigned long previousMillis1 = 0; int ledState1 = LOW; // Start LED1 OFF. const int ledPin2 = 12; // This can be set to any one of the Digital I/O pins. long TimeOn2 = 300; // Fine LED Control: milliseconds LED Is On.
long TimeOff2 = 450; // Fine LED Control: milliseconds LED Is Off. unsigned long previousMillis2 = 0; int ledState2 = HIGH; // Start LED2 ON. void setup() {
pinMode(ledPin1, OUTPUT); // Put 'ledPin1' I/O Pin in OUTPUT mode. pinMode(ledPin2, OUTPUT); // Put 'ledPin2' I/O Pin in OUTPUT mode.
} void loop() { unsigned long currentMillis = millis(); // Creat variable currentMillis, put current time in variable (reset timing process). // IF the difference between current-time and last-time executed is greater than interval-time). // then execute task; change state of I/O pin. if ( (ledState1 == HIGH) && (currentMillis - previousMillis1 >= OnTime) ){
ledState1 = LOW; // Set state to LOW. previousMillis1 = currentMillis; // Save current time.
digitalWrite(ledPin1, ledState1); // Write to I/O pin; Turns LED OFF.
} else if ((ledState == LOW) && (currentMillis - previousMillis >= OffTime)) { ledState1 = HIGH; // Set state to HIGH.
previousMillis1 = currentMillis; // Save current time.
digitalWrite(ledPin1, ledState1); // Write to I/O pin; Turns LED ON.
} if ( (ledState2 == HIGH) && (currentMillis - previousMillis2 >= OnTime) ){
ledState2 = LOW; // Set state to LOW. previousMillis2 = currentMillis; // Save current time.
digitalWrite(ledPin2, ledState2); // Write to I/O pin; Turns LED OFF.
} else if ((ledState2 == LOW) && (currentMillis - previousMillis >= OffTime)) { ledState2 = HIGH; // Set state to HIGH.
previousMillis2 = currentMillis; // Save current time.
digitalWrite(ledPin2, ledState2); // Write to I/O pin; Turns LED ON.
} }

Add more 'state machines' until there is no more available memory or GPIO pins.

Each 'state machine' runs independant of each other; parellel processing.  As an exercise, edit the code above to add a third state machine.

There are better ways to manage this complexity.  There are programming techniques simpler and more efficient.


More.......

There are better ways to manage the complexity demostrated above.   There are programming techniques simpler and more efficient.   I introduce these in a subscription access to the inner online lab.  More advanced features of the Arduino programming language and indepth Atmel AVR programming are presented and hired consulting and engineering provided through professional service.


Arduino Open Source Community member - Tier 1 Full Stack Developer

'IoT & Embedded MAKER SPACE - MKE'

CONACT to learn about a new group in METRO Area of WI & IL - Milwaukee, Racine, Kenosha, South Eastern WI And Northern IL, space and/or spaces - Forming Now!

We Have Industrial Zoned Making Space With Outfitted Machine Shop And Lab Space Available!  Multiple Dedicated Online Locations Available In Milwaukee 3rd Ward.

I am building a network of creative Makers/Doers!... Come Learn More, Join, Engage, Enjoy, Create A Solid Future Of Fun And Reward... Come Help..... Core Team Members Wanted, Or Simply be A Hangout Member Learning, Socializing/Networking... Start-Up Incubator Space, Students Tutoring Space, Retired... Hobbyists, Educators, Scientists, Researchers, Developers...

Mega Industrial Machines Building!!!!  AI Development.... Super Computing.... @ Tier 1 Level.  Locations looking to establish a network space, include Chicago IL, Melbourne Vic Australia.


Use The Technology Wisely & Keep It Simple

- Cheers!

Gene Casanova


Need More?   Need Help? ........Software, Network, System & Data Center Builder & Developer | Development Services Available - Freelance Small Jobs; To Outsourced Long Term Service Provider Contract Labor Available.

CGI Computer Wares | EST 1979

Send E-MailCONTACT: [click]@CGIComputerWares.com