Willkommen auf unserem Seminar-Blog

Immer auf dem aktuellen Stand bleiben

Dieser Seminar-Blog befindet sich noch im Aufbau und wird in den kommenden Tagen entsprechend verfeinert.

Member Login

Lost your password?

Registration is closed

Sorry, you are not allowed to register by yourself on this site!

You must either be invited by one of our team member or request an invitation by email at viad.info {at} zhdk {dot} ch.

Code

17. December 2010

To create a relatively simple but working navigationsystem we wrote an Android application. Android, because we could access GPS data and XML on a webserver using the mobile phone connection. To establish communication between Android and Arduino we used Amarino. Amarino is a Libary that it allows a connection between an Android application and an Arduino over Bluetooth. The use of Amarino is relatively simple and quick to set up. There are two ways to use Amarino. First, you can write a plug-in for Amarino, the second way is to create a standalone application with the additional functionality of Amarino. We have decided to write a plug-in. The reason to do this is because the handling and programming is a lot simpler Based on the "Time" plug-in which is available as an example to the Amarino website, we have adapted our code accordingly. We decided to not use Amarino anymore. We used Bluetooth to communicate between Android and Arduino in Android directly. We think this way is cleaner because it reduces the functions to a minimum. Our code is based on the Bluetooth Chat Example from Android.com. There are two Android Examples available: 1. Navigation System 2. Demo Example (can be a good starting point if you want to build your own Android 2 Arduino Example) PHP Script But lets start in the beginning. To get the routing we used OpenRouteService. Since the OpenRouteService XML data could not directly address in XML, we have built a small PHP script that allows to simplify the desired route of OpenRouteService.org and load it up in the right format to a webserver. To parse and rebuild the XML we used SimpleXML from PHP.
<?php

//  5MB maximum file size
$MAXIMUM_FILESIZE = 5 * 1024 * 1024;
//  Valid file extensions (images, word, excel, powerpoint)
$rEFileTypes =
  &quot;/^\.(jpg|jpeg|gif|png|doc|docx|txt|rtf|pdf|xls|xlsx|
        ppt|pptx){1}$/i&quot;;
$dir_base = &quot;/home/httpd/vhosts/alaingroeneweg.com/subdomains/gps/httpdocs&quot;;

$isFile = is_uploaded_file($_FILES['userfile']['tmp_name']);


if($isFile)    //  do we have a file?
{   
	//userfile is the name of the upload_button
   move_uploaded_file($_FILES['userfile']['tmp_name'], $dir_base.&quot;/route.xml&quot;);
   parseXml();
}





function parseXml(){
	
	$xml = simplexml_load_string(file_get_contents('route.xml'));
	
	$new_xml = simplexml_load_string('');
	
	foreach ($xml-&gt;xpath('//xls:RouteInstruction') as $route_instr)
	{	
		
		$instructionArr = $route_instr-&gt;xpath('xls:Instruction');
		$instruction = $instructionArr[0];
		$posArr = $route_instr-&gt;xpath('xls:RouteInstructionGeometry/gml:LineString/gml:pos');
		$pos = $posArr[0];
				
		//two variables out of one through splitting the string at the space	
		list($long, $lat) = explode(' ', $pos);

	
			
		if (preg_match("/\blinks\b/i", $instruction))
		{
   			$location = $new_xml-&gt;addChild('location');
   			$location-&gt;addChild('longitude',$long);
   			$location-&gt;addChild('latitude',$lat);
   			$location-&gt;addChild('direction',"1");
   		}
   		if (preg_match("/\bhalb links\b/i", $instruction))
		{
		 	$location = $new_xml-&gt;addChild('location');
			$location-&gt;addChild('longitude',$long);
   			$location-&gt;addChild('latitude',$lat);
   			$location-&gt;addChild('direction',"2");
		}
		if (preg_match("/\brechts\b/i", $instruction))
		{
			$location = $new_xml-&gt;addChild('location');
			$location-&gt;addChild('longitude',$long);
   			$location-&gt;addChild('latitude',$lat);
   			$location-&gt;addChild('direction',"3");
		}
		if (preg_match("/\bhalb rechts\b/i", $instruction))
		{
		 	$location = $new_xml-&gt;addChild('location');
			$location-&gt;addChild('longitude',$long);
   			$location-&gt;addChild('latitude',$lat);
   			$location-&gt;addChild('direction',"4");
		}
		
		
	}


$dom = new DomDocument("1.0");
$dom-&gt;loadXML($new_xml-&gt;asXml());
$dom-&gt;encoding="iso-8859-1";
htmlspecialchars($dom-&gt;saveXML());
$dom-&gt;save("minimal.xml");
chmod("minimal.xml",0777);

}






   
?> 


<head>
	
		<title>Routeuploader</title>
		
</head>	
<bod>	
	<h1>Bike-Navigation</h1>
	<a id="link" href="http://openrouteservice.org/">download openrouteservice-xml here</a>
	<!-- The data encoding type, enctype, MUST be specified as below -->
		<form enctype="multipart/form-data" method="POST">
    		<!-- MAX_FILE_SIZE must precede the file input field -->
    		
   			 <!-- Name of input element determines name in $_FILES array -->
			 Send this file:<br />
   		 	
		</form>
	</body>

Android Script Amarino is a Android Application. Since it is a native of Android application, the Android Developer SDK is required. For our tests, we used a HTC with Android Hero Europe Edition with Android 2.1. SAXParser By using SAX parser, we could easily access the XML file that has been uploaded by our PHP script and retrieve corresponding values. GPS as a singleton It seems that the Amarino Plugin doens’t allow to have any Activities on runtime. The GPS on the other hand requires an activity. To solve this problem we built the GPS part as a Singleton and started its activity before the plug-in runs as a Background service. This way we could access the GPS data al the time in our Android plug-in. Amarino Our Amarino plugin reads the XML data from the SAX parser and compares the coordinates with the GPS values. If the GPS data is close to the coordinades form the XML is reads the direction and sends the direction to the Arduino using Bluetooth. Arduino The Arduino microcontroller recieves the sent values from the Amarino plug-in and starts a vibrations pattern for the handlebars. For each direction we have two different pattern.
// This Code Reads the Serial Data whoch comes from Android
// 2009 mlab.uiah.fi/paja
// Echoing program (Return byte that is recieved from serial.
#include <MeetAndroid.h>

MeetAndroid meetAndroid;

char incomingByte = 0;	// for incoming serial data




// ------ DIRECTION PINS INIT ---------------------

int pinArrayLeft[] = {12, 13, 14};
int pinArrayLittleLeft[] = {12, 13};
int pinArrayRight[] = {2, 3, 4};
int pinArrayLittleRight[] = {2, 3};
// ------------------------------------------------
int timer = 100;
int timerLittle =400;
int count = 0;


void setup() {
  // Start serial communication
  Serial1.begin(115200);
  Serial.begin(57600);
  
  for (count=0;count<3;count++) {
    //////////////// LINKS
    pinMode(pinArrayLeft[count], OUTPUT);
    pinMode(pinArrayLittleLeft[count], OUTPUT);
    //////////////// RECHTS
    pinMode(pinArrayRight[count], OUTPUT);
    pinMode(pinArrayLittleRight[count], OUTPUT);
  }
}

void loop() {
  
  while (Serial1.available() > 0) 
  {

    byte serialreadbytevalue = Serial1.read();
    Serial.println(serialreadbytevalue);
    
    switch(serialreadbytevalue)
    {
      case 49:
        goLeft();
        break;
      case 50:
        goLittleLeft();
        break;
      case 51:
        goRight();
        break;
      case 52:
        goLittleRight();
        break;
    }
  }
 
}

void goLeft() {
   
  
  for (count=0;count<3;count++) {
   digitalWrite(pinArrayLeft[count], HIGH);
   delay(timer);
   digitalWrite(pinArrayLeft[count], LOW);
   delay(timer);
  }
  
}

void goRight() {
  for (count=0;count<3;count++) {
   digitalWrite(pinArrayRight[count], HIGH);
   delay(timer);
   digitalWrite(pinArrayRight[count], LOW);
   delay(timer);
  }
}
void goLittleLeft() {
  for (count=0;count<2;count++) {
   digitalWrite(pinArrayLittleLeft[count], HIGH);
   delay(timerLittle);
   digitalWrite(pinArrayLittleLeft[count], LOW);
   delay(timerLittle);
  
  }
}

void goLittleRight() {
  for (count=0;count<2;count++) {
   digitalWrite(pinArrayLittleRight[count], HIGH);
   delay(timerLittle);
   digitalWrite(pinArrayLittleRight[count], LOW);
   delay(timerLittle);
  
  }
}