Real-Time Traffic Project

In this forum you can share tips and tricks on how to use Cinemar products with some program or a task that you want it to do. So no one has to reinvent the wheel if you already did! (No questions in this forum only solutions, questions will be deleted without hesitation.)

User avatar
ronsatter
Is there life beyond Cinemar?
Is there life beyond Cinemar?
Posts: 1304
Joined: Mon Dec 04, 2006 1:40 am
Location: San Leandro, CA
Contact:

Real-Time Traffic Project

Postby ronsatter » Sat Jan 10, 2009 11:37 pm

I recently added a real-time dynamic traffic info display to my "Home" scene. See FIG 1

Although this scene is custom designed and is NOT found in the default ML scene packages, all of the scene objects for construction of this project are in the default ML graphic packages.

This project is intended to give a practical example of how to use several excellent ML plugins and design ideas to achieve a real-time data display and uses some fairly advanced concepts in ML plugins and command integration. You should NOT attempt this project if you do not have a good grasp of ML fundamentals. That being said, I offer this "How To", hopefully with enough details to get you started and give you some inspiration.

:!: IMPORTANT: This project builds a scene which incorporates the visual objects at the bottom banner area where you see the label "TRAFFIC" ... NOT THE ENTIRE HOME SCENE.

See FIG 2 for the "Traffic.mls" scene, the end result of this project. The objects in this scene are intended to be incorporated into the "Navigation" or "Menu" layers of your ML scenes, so that they remain visible throughout scene changes. My "Home" scene incorporates the objects in this project as part of the top-most layer (4) on all my scenes. For help loading this scene as an "Overlay", consult threads in the forum and the wiki.

Description

To the right of the label "TRAFFIC" is a tinted button, followed by some text detailing a traffic source by title, and it's real-time "jam factor" or traffic flow rate. The button color updates with every traffic source change and gives simple visual feedback of the traffic flow at a glance. "Normal" is green, "Slow" is yellow and "Jammed" is red. The display changes traffic sources every 10 secs and cycles through a custom list automatically as long as MLServer is running. The direction arrows to the left of the label "TRAFFIC" allow manual forward/back stepping through the traffic sources for quick reference.

Plugins needed

MLRSS
MLCycleStates
MLTimer
MLStrings
MLMath
MLConditional
MLStartup

1. Choose a traffic feed

The RSS traffic feed source used in this project is from a website called "Traffic.com"
URL: http://www.traffic.com

:!: IMPORTANT: If you don't use an RSS feed from "Traffic.com" you won't have the word "JAMFACTOR" or it's value in your data string. This is mandatory for the proper completion of this project

On the bottom of the home page of this site, click "RSS". You should see "Free RSS Feeds By City" and a list of US cities. If you happen to live in one of these listed metro areas, you're in luck and can continue this project. Find the feed for your metro area and right-click on the RSS orange icon and select "Properties". This will bring up a window with the feed address link you will need for the new "Traffic" feed you will create in MLRSS. Copy the address listed in the "Link Properties" window.

2. Add RSS feed to MLRSS Plugin

Open the MLRSS plugin "Settings" window and click "ADD". Type "Traffic" in the "Alias" field and paste the copied feed address link into the "URL" field. Click "OK" to add the feed to the plugin. You should see your new feed called "Traffic" in the "Current Feeds" list of the plugin. Click on the feed name in the list then click "Update". You should see traffic related data in the "Feed Info" area to the right. Use the "Next" / "Previous" buttons below to move through the various feed items. See FIG 3

The info you see listed here is available in variables in MLServer, and can be displayed on your scene. See the MLRSS plugin wiki for details.

Now that you have the RSS feed data coming into MLServer through the MLRSS plugin, the parts of the data relating to "JAMFACTOR" can be parsed from the data string for use in determining the associated color tint. This requires some commands using the MLStrings, MLMath and MLConditional plugins.

3. Create data parsing commands

Data string parsing is not easy for me, so I called on the author of the MLStrings plugin, Brandon Higa for help. The task was to find a way to isolate the "JAMFACTOR" number from the data string and to assign it a color value corresponding to the number's value.

Are you still with me here? :shock: Yeh, that's where Brandon stepped in. The following commands are from Brandon Higa:

Create a new ML command macro called "Traffic_JamFactor"

Paste in this code:

Code: Select all

Macro|
MLStrings|INSTR~traffic_jamfactor_string_start~{{traffic_source}}~JAMFACTOR!
MLMath|ADD~traffic_jamfactor_number_start~{{traffic_jamfactor_string_start}}~10!
MLStrings|LEN~traffic_jamfactor_text_length~{{traffic_source}}!
MLMath|SUBTRACT~traffic_jamfactor_number_length~{{traffic_jamfactor_text_length}}~{{traffic_jamfactor_number_start}}~-1!
MLStrings|RIGHT~traffic_jamfactor_number~{{traffic_source}}~{{traffic_jamfactor_number_length}}!
Traffic_JamFactor_Color


All the lines of this command macro (except the last) are used to isolate a number value which follows the word JAMFACTOR. Once we have that value, we call the last line in this command (another macro) to determine the associated color for that number value.

Create another ML command macro called "Traffic_JamFactor_Color"

Paste in this code:

Code: Select all

Macro|
SetVariable|traffic_jamfactor_color~Green!
MLConditional|IsGTE##{{traffic_jamfactor_number}}##3.3##SetVariable|traffic_jamfactor_color~Orange!
MLConditional|IsGTE##{{traffic_jamfactor_number}}##6.6##SetVariable|traffic_jamfactor_color~Red


This code evaluates the JAMFACTOR value (a range from 0.0 to 10.0) and assigns it a color according to the range. ie. 0 to 3.3 is "GREEN" 3.3 to 6.6 is "ORANGE" and 6.6 to 10.0 is "RED". The correct color is set in a variable called {{traffic_jamfactor_color}} and is used to determine the color of the button displayed in the scene.

4. Create new ML scene

Create a "New" scene in Main Lobby and "Save As" "Traffic.mls". You must create 7 objects in the scene:

- Left and Right direction arrows to step through the traffic feed items
- A static label called "TRAFFIC"
- Three color code buttons
- A "Text.Sizeable" button to display the feed item data

Buttons 1 and 2

Use the standard ML button object 0019_0057i for the "Left" arrow button
Use the standard ML button object 0019_0057j for the "Right" arrow button

Paste this MLServe.Cmd into the "Left" arrow button:

Code: Select all

MLCycleStates|Prev~Traffic_Feeds

Paste this MLServe.Cmd into the "Right" arrow button:

Code: Select all

MLCycleStates|Next~Traffic_Feeds

These arrow nav buttons use the MLCycleStates Plugin, to navigate through a group of items in the plugin called "Traffic_Feeds". (We will create the MLCycleStates variable later). It allows the user to navigate through the various feed items at any time.

Use the standard ML button object 0019_0055 for the color coded buttons. Create three buttons with these settings in the button properties panel window:

Button 3

Label: NORMAL{{traffic_jamfactor_color}}
Font Style: AriBold
Font Size: 16
Font Color: Black
Size: 70%
Check "Hide Value"
Set OFF STATE color to GREEN
Set local OFF STATE value to GREEN
Set ON STATE alpha to 0

Button 4

Label: SLOW{{traffic_jamfactor_color}}
Font Style: AriBold
Font Size: 16
Font Color: Black
Size: 70%
Check "Hide Value"
Set OFF STATE color to ORANGE
Set local OFF STATE value to ORANGE
Set ON STATE alpha to 0

Button 5

Label: JAMMED{{traffic_jamfactor_color}}
Font Style: AriBold
Font Size: 16
Font Color: Black
Size: 70%
Check "Hide Value"
Set OFF STATE color to RED
Set local OFF STATE value to RED
Set ON STATE alpha to 0

These three buttons are stacked directly on top of each other (same xy coordinates) in the scene. Only one of the three buttons will be visible for each traffic feed item, depending on the evaluation of the JAMFACTOR color, and the other two buttons will be invisible. This is accomplished by the "Button State" settings assigned above.

Button 6

Use a "Text.Sizeable" button for the traffic feed item data

Font Style: FuturaTMed
Font Size: 20
Font Color: White
Label:{{Traffic_Source}}~500~28

This should be long enough for your feed item data, if it isn't, change the number "500" in the label to something greater.

Button 7

Use a "Text.Sizeable" button for the static "TRAFFIC" label

Font Style: FuturaTMed
Font Size: 20
Font Color: Yellow
Label:TRAFFIC~80~28

This completes all of the necessary objects for the scene. Remember to stack the color buttons. Click "Save" to update the scene. See FIG 2

5. Create MLCycleStates Variable

Depending on your metro area, there may be more traffic feed items than you wish to view. Fortunately, we can filter the items by creating a "States Variable" in MLCycleStates which will group only the feed items we wish to view. MLCycleStates provides a way to build a list of MLCommands which can be executed as you "Cycle" through the items in the list. The "Arrow" buttons we built for our scene contain MLCycleStates commands:

Code: Select all

MLCycleStates|Prev~Traffic_Feeds

Code: Select all

MLCycleStates|Next~Traffic_Feeds


As you can see, these commands look in the MLCycleStates plugin for the states variable named "Traffic_Feeds" and execute the "Next" or "Previous" item in the list. So what we build in the MLCommand area of each item in the list will get executed.

Take a look at the screenshot of my MLCycleStates plugin window. See FIG 4

It shows the "Traffic_Feeds" states variable with the listed items, their labels and MLCommands. I have numerous items in my list. Technically you can have as few as two items, and the "Cycle" function will work.

Open the MLCycleStates plugin "Settings" window and click "ADD NEW". Type "Traffic_Feeds" in the "Variable Name" field. Click "DONE" to add the variable to the plugin. The variable "Traffic_Feeds" should appear in the list of "States Variables" on the left side of the plugin window.

Click on "Traffic_Feeds" to highlight it, then click on "Click here to add a new state" in the right side of the plugin window. A new row in the list is created, waiting for a label name and an MLCommand for the item.

Type a label which makes sense to you for the first item, ie "GG Bridge - Inbound".

Paste this command macro in the MLServe Command column:

Code: Select all

Macro|SetVariable|Traffic_Source~{{mlrss_feed_010_item_024_title}}!Traffic_JamFactor


Notice the variable in the command {{mlrss_feed_010_item_024_title}}

The feed number 010 and item title number 024 need to be changed to your feed and item title numbers from your MLRSS feed called "Traffic".

For example, if your MLRSS Feed number for "Traffic" was "001" and you wanted to view the first item title in the feed, then your entry in the MLCycleStates ML Command column for the first item would have this code:

Code: Select all

Macro|SetVariable|Traffic_Source~{{mlrss_feed_001_item_001_title}}!Traffic_JamFactor


Once you have created the first entry in the list, you can copy and paste to add new entries in the list, then just change the item title number 001 to whatever item title you want to see in the cycle. Refer to your MLRSS "Traffic" feed to view which feed items you wish to add to the MLCycleStates states variable "Traffic_Feeds".

:!: IMPORTANT: Constructing this list in MLCycleStates correctly is crucial to proper execution of this project.

Use FIG 4 as a guide. The code is similar for each item. Basically, all that changes is the "Label" entry and the "Item Title Number" in the variable. Be sure to click "Done" at the bottom of the MLCycleStates Plugin window when you have completed your entries.

6. Create MLTimer Event

One of the nice features of this project involves use of MLTimer Plugin. We create a dedicated timer event which does nothing but cycle through the "Traffic_Feeds" list we just constructed. The timer event will be started automatically whenever MLServer starts (Using the MLStartup Plugin), and will run continuously in the background, changing the traffic data on the scene every 10 secs. This is not CPU intensive and should not impact any performance of ML.

Open the settings window of MLTimer Plugin and click the "Timer Setup" tab. Select a "Timer To Edit" from the drop down menu and fill out the "Timer Properties" section of the window, using FIG 5 as a guide.

Set "Interval" to 10 seconds. Set "Mode" to "Repeating"

Paste this code in the MLServeCmd area:

Code: Select all

MLCycleStates|Next~Traffic_Feeds

Be sure to click "Save" before you close the window.

7. Create MLStartup command

Open the settings window of MLStartup Plugin and past this command in the window: See FIG 6

Code: Select all

Enable~Timer4


This code has the timer number for MY MLTimer Event (4). Replace the 4 with YOUR MLTimer event number you created in step 6.

Click "CLOSE" to save settings.

8. Test Run

Restart MLServer to enable the MLTimer Event which cycles through the MLRSS Traffic feeds data.

Open the scene "Traffic.mls" and watch for the traffic data to change. It should change every 10 secs and cycle through the list of traffic feed items you constructed in the MLCycleStates Plugin. Because the MLTimer event is set to "Repeat", it will cycle as long as MLServer is running, or the Timer Event is disabled or deleted.

If the JAMFACTOR number is between "0.0" and "3.3" you should see a "GREEN" button with label "NORMAL"
If the JAMFACTOR number is between "3.3" and "6.6" you should see an "ORANGE" button with label "SLOW"
If the JAMFACTOR number is between "6.6" and "10.0" you should see a "RED" button with label "JAMMED"

Remember that the MLRSS Plugin allows you to set the "Update" frequency for each feed. I have my "Traffic" feed set to update every 5 minutes. In my area, traffic conditions can change very quickly during commute time, so I use a shorter frequency. Set yours accordingly.

If you get stuck with this project or have any questions, please reply to this thread so others may benefit and learn.

Good Luck :D

Ron Satter
You do not have the required permissions to view the files attached to this post.
Last edited by ronsatter on Mon Jan 12, 2009 1:30 pm, edited 1 time in total.
If it ain't broke ... don't fix it!

User avatar
DaveB
Simply Incredible
Simply Incredible
Posts: 3143
Joined: Sun May 30, 2004 10:57 pm
Location: Mays Landing, NJ
Contact:

Postby DaveB » Sun Jan 11, 2009 10:16 am

Ron;
You are doing some great work!! I like your methods of parsing, which is actually the entire basis of the "Module" you have created.

I am just happy that I do not need a traffic report in Southern NJ, In fact for where I live, one is not even available!
Thanks!

Dave Bruner
:shock:

User avatar
ronsatter
Is there life beyond Cinemar?
Is there life beyond Cinemar?
Posts: 1304
Joined: Mon Dec 04, 2006 1:40 am
Location: San Leandro, CA
Contact:

UPDATE

Postby ronsatter » Mon Jan 12, 2009 1:54 pm

I noticed a small annoyance with the variable naming in the data parsing command, the variables now have been renamed.

Now the variables all start with the word "traffic" so they are easier to find in MLServer Variables window. The code functions the same ... only the "names have been changed to protect the innocent" 8)

Here is the update:

In your existing macro called "Traffic_JamFactor", DELETE the existing code and paste in this code:

Code: Select all

Macro|
MLStrings|INSTR~traffic_jamfactor_string_start~{{traffic_source}}~JAMFACTOR!
MLMath|ADD~traffic_jamfactor_number_start~{{traffic_jamfactor_string_start}}~10!
MLStrings|LEN~traffic_jamfactor_text_length~{{traffic_source}}!
MLMath|SUBTRACT~traffic_jamfactor_number_length~{{traffic_jamfactor_text_length}}~{{traffic_jamfactor_number_start}}~-1!
MLStrings|RIGHT~traffic_jamfactor_number~{{traffic_source}}~{{traffic_jamfactor_number_length}}!
Traffic_JamFactor_Color


The tutorial has been updated to reflect this change.

Ron
If it ain't broke ... don't fix it!


Return to “Tips, Tricks and Tutorials”

Who is online

Users browsing this forum: No registered users and 1 guest