Difference between revisions of "CSC231 Lab 6"

From dftwiki3
Jump to: navigation, search
(Getting the Messenger Library for the Arduino)
(Open Problems)
 
(31 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
=CSC 231 Lab # 6=
 
=CSC 231 Lab # 6=
  
[[Image:ArduinoStamp.jpg | right]]
 
 
© D. Thiebaut, 2008
 
© D. Thiebaut, 2008
  
 
In this lab we are going to make the Arduino keep on listening to commands it gets from the assembly language on the PC, and execute each command as it receives them.
 
In this lab we are going to make the Arduino keep on listening to commands it gets from the assembly language on the PC, and execute each command as it receives them.
  
==Getting the Messenger Library for the Arduino==
+
==Getting the SimpleMessageSystem Library for the Arduino==
 +
[[Image:ArduinoCGI.jpg | right | 200px]]
  
You need a new library for the Arduino called '''[http://www.arduino.cc/playground/Code/Messenger Messenger]'''  
+
You need a new library for the Arduino called '''[http://www.arduino.cc/playground/Code/SimpleMessageSystem SimpleMessageSystem]''' (Note: there is a newer version of it, Messenger, but I haven't been able to play with it enough to make it the version to use for the project.)
  
 
* Boot the Ubuntu CD-Rom
 
* Boot the Ubuntu CD-Rom
Line 22: Line 22:
 
* download the library file from the Arduino Playground:
 
* download the library file from the Arduino Playground:
  
   wget http://www.arduino.cc/playground/uploads/Code/Messenger.zip
+
   wget http://www.arduino.cc/playground/uploads/Code/SimpleMessageSystem.zip
  
 
* unzip the file
 
* unzip the file
  
   unzip Messenger.zip
+
   unzip SimpleMessageSystem.zip
  
 
* Remove the archive file
 
* Remove the archive file
  
   rm Messenger.zip
+
   rm SimpleMessageSystem.zip
  
 
* Set the ownership of the new library the same ownership as that of the arduino software
 
* Set the ownership of the new library the same ownership as that of the arduino software
  
   chown -R 1000:1000 Messenger
+
   chown -R 1000:1000 SimpleMessageSystem
 +
 
 +
* Open the arduino GUI
 +
  cd ../..
 +
  .arduino
 +
 
 +
* Create this new sketch. 
  
* Open the arduino GUI and create this new sketch
 
 
<code><pre>
 
<code><pre>
// This example sets all the values of the digital pins with a list through a callback function
+
/*
 +
---- SimpleMessageSystem Example 1 ----
 +
Control Arduino board functions with the following messages:
 +
 +
r a -> read analog pins
 +
r d -> read digital pins
 +
w d [pin] [value] -> write digital pin
 +
w a [pin] [value] -> write analog pin
 +
 +
Base: Thomas Ouellet Fredericks
 +
Additions: Alexandre Quessy 
 +
*/
  
#include <Messenger.h>
+
// Include de SimpleMessageSystem library
// Instantiate Messenger object with the default separator (the space character)
+
// REMOVE THE FOLLOWING LINE IF USING WIRING
Messenger message = Messenger();
+
#include <SimpleMessageSystem.h>  
  
// Create the callback function
+
void setup() {
void messageReady() {
+
  // The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!!
    int pin = 0;
+
  // I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that's 12x faster
      // Loop through all the available elements of the message
+
  Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!
      while ( message.available() ) {
 
// Set the pin as determined by the message
 
        digitalWrite( pin, message.readInt() );
 
        pin=pin+1;
 
      }
 
 
}
 
}
  
 +
void loop() {
 +
  if (messageBuild() > 0) { // Checks to see if the message is complete and erases any previous messages
 +
    switch (messageGetChar()) { // Gets the first word as a character
 +
    case 'r': // Read pins (analog or digital)
 +
      readpins(); // Call the readpins function
 +
      break; // Break from the switch
 +
    case 'w': // Write pin
 +
      writepin(); // Call the writepin function
 +
    }
 +
  }
 +
}
  
void setup() {
+
void readpins(){ // Read pins (analog or digital)
   // Initiate Serial Communication
+
   switch (messageGetChar()) { // Gets the next word as a character
  Serial.begin(115200);  
+
    case 'd': // READ digital pins
  // Attach the callback function to the Messenger
+
      messageSendChar('d');  // Echo what is being read
   message.attach(messageReady);
+
      for (char i=2;i<14;i++) {
 +
        messageSendInt(digitalRead(i)); // Read pins 2 to 13
 +
      }
 +
      messageEnd(); // Terminate the message being sent
 +
      break; // Break from the switch
 +
   case 'a': // READ analog pins
 +
      messageSendChar('a');  // Echo what is being read
 +
      for (char i=0;i<6;i++) {
 +
        messageSendInt(analogRead(i)); // Read pins 0 to 5
 +
      }
 +
      messageEnd(); // Terminate the message being sent
 +
  }
 
}
 
}
  
 +
void writepin() { // Write pin
 +
  int pin;
 +
  int state;
  
void loop() {
+
  switch (messageGetChar()) { // Gets the next word as a character
   // The following line is the most effective way of using Serial and Messenger's callback
+
   case 'a' : // WRITE an analog pin
  while ( Serial.available() )  message.process(Serial.read () );
+
    pin = messageGetInt(); // Gets the next word as an integer
 +
    state = messageGetInt(); // Gets the next word as an integer
 +
    pinMode(pin, OUTPUT); //Sets the state of the pin to an output
 +
    analogWrite(pin, state); //Sets the PWM of the pin
 +
    break;  // Break from the switch
 +
   
 +
  case 'd' : // WRITE a digital pin
 +
    pin = messageGetInt();  // Gets the next word as an integer
 +
    state = messageGetInt();  // Gets the next word as an integer
 +
    pinMode(pin,OUTPUT); //Sets the state of the pin to an output
 +
    digitalWrite(pin,state);  //Sets the state of the pin HIGH (1) or LOW (0)
 +
  }
 
}
 
}
 +
  
 
</pre></code>
 
</pre></code>
  
* Compile and download it to your Arduino
+
* Save your sketch in the /root/sketches folder, and give it the name '''loop'''.  I found that saving is not always straightforward, and it works best for me if I use the right arrow in the tab area of the Arduino GUI, click it, and use the rename option.
  
* I M P O R T A N T
+
* IMPORTANT
 
::{|  
 
::{|  
 
| style="background:orange; color:darkblue" | '''Note''': When you boot your PC in Ubuntu mode from the live CD, you are working with a Linux computer that exists only on the CD and in the RAM.  Whatever you create or copy to your ubuntu or root account exists only in RAM.  As soon as you close the system, all your changes disappear.  All the files you created disappear.  For this reason, you will need a USB key to save all the files you create, including sketches and libraries, such as the Messenger library you created above.
 
| style="background:orange; color:darkblue" | '''Note''': When you boot your PC in Ubuntu mode from the live CD, you are working with a Linux computer that exists only on the CD and in the RAM.  Whatever you create or copy to your ubuntu or root account exists only in RAM.  As soon as you close the system, all your changes disappear.  All the files you created disappear.  For this reason, you will need a USB key to save all the files you create, including sketches and libraries, such as the Messenger library you created above.
 
|}
 
|}
  
==Assembly Part==
+
:Thus, at this point your''' /root''' directory should contain two folders: '''arduino-0010''' and '''scketchbook''', and sketchbook should have a new sketch called '''loop'''.  You may want to '''drag''' the whole root folder to your USB memory stick and save all these new modifications.
 +
<br />
 +
<center>
 +
[[Image:CSC231 Ubuntu Root Folder.png]]
 +
</center>
 +
 
 +
* Use the Tools menu to set the board as the '''Diecimila''', and the port as the '''USB''' port.
 +
 
 +
* Compile. You may get warnings, but make sure you get a compile confirmation:
 +
 
 +
  Binary sketch size: 3402 bytes (of a 14336 byte maximum)
 +
 
 +
* Upload to the Arduino.  Check that the Receive and Transmit LED on the board light up to indicate data transfer.
 +
 
 +
 
 +
[[Image:UbuntuPinguin.png | right | 200px ]]
 +
<br />
 +
<br />
 +
<br />
 +
<br />
  
* Open a Terminal window under Ubuntu and type
+
==Assembly Part on the PC==
 +
 
 +
===OUTPUTTING INFORMATION===
 +
====LedOn====
 +
 
 +
You are going to create a program called '''ledOn''' that turns on the LED connected to Pin 13.
 +
 
 +
* Open a new Terminal window under Ubuntu and type
 
   sudo su
 
   sudo su
 
   cd
 
   cd
* Highlight the contents of the file [[CSC231 driver.c | driver.c]] and type Control-C to copy it to the clipboard
+
* Highlight the contents of the file [[CSC231 driver.c | driver.c]] and copy it to the clipboard
 
* In the terminal window type
 
* In the terminal window type
  
Line 94: Line 168:
 
** [[CSC231 asm_io.asm | asm_io.asm ]]
 
** [[CSC231 asm_io.asm | asm_io.asm ]]
 
** [[CSC231 ledOn.asm | ledOn.asm ]]
 
** [[CSC231 ledOn.asm | ledOn.asm ]]
 +
 +
:(You may want to copy these to your memory stick as well before you forget!!!)
 +
 +
* Take a look at LedOn.asm.  We'll look over the main features in class.  It is important to note where the message containing the command is, and what its format is.
 +
 +
::{|
 +
| style="background:orange; color:darkblue" | '''Note 2''': Make sure the variable USB in ledOn.asm contains the same string identifying the USB port as reported in the  Arduino GUI.
 +
|}
 +
 +
* Assemble/compile the files:
 +
 +
  nasm -f elf -F stabs asm_io.asm
 +
  nasm -f elf -F stabs ledOn.asm
 +
  gcc -o ledOn driver.c ledOn.o asm_io.o
 +
 +
* Run the program
 +
 +
  ./ledOn
 +
 +
::Does the LED on Pin 13 turn ON?  (If your Pin 13 is damaged from previous labs, please put a resistor plus LED connected to Pin 11).
 +
 +
====LedOff====
 +
 +
Ok, you figured it out.  Make a copy of ledOn.asm under the name ledOff.asm, modify it, and compile it.  Use it to turn OFF the LED.
 +
 +
Remember to check the [http://www.arduino.cc/en/Reference/HomePage Arduino Reference Page] if you need to debug your sketch...
 +
 +
====LedOnOff====
 +
 +
Same scenario, but now this time make your assembly program send an ON message, followed by an OFF message in an endless loop.
 +
 +
Measure the frequency of the LED turning ON and OFF.  This will give us an idea of how fast we can activate devices attached to the Arduino.
 +
 +
How could we make the LED blink faster, but still be able to control this blinking from the assembly program?  If you have time, try to implement a solution.
 +
 +
For those interested creating sound, the human ear can hear sounds between about 300 and 20,000 Hz.
 +
 +
<!--
 +
===INPUTTING INFORMATION===
 +
 +
This part is for the whole class to devise as a group first, and when a solution has been figured out, to implement in pairs.
 +
 +
* Figure out a way to connect a switch to the Arduino.  Please please make sure I have a chance to review your design on paper before  you wire it up so as not to  '''damage''' the Arduino or its input/output pins.
 +
 +
* Then write an assembly language program that continuously reads the status of the switch and displays it on the screen as a long series of 0s or 1s.
 +
 +
-->
 +
 +
=How to copy your environment back to the Ubuntu PC=
 +
 +
When you want to resume work on your Arduino, you need to copy your environment back from the USB stick to the /root folder.  If you try to drag and drop the folder from the USB stick to your /root folder, it will not work.  This is because the desktop belongs  by default to User Ubuntu, but you want to modify the folder of User Root, and Ubuntu won't allow you to do that.
 +
 +
The solution is to right-click on the /root folder, select property, and set its permissions to read/write for ''user'', for ''group'', and for ''others''. 
 +
 +
Then the drag and drop operation will work.
 +
 +
=Open Problems=
 +
 +
Things your instructor doesn't know, but that you might be able to discover while playing with the Arduino, and in which case you are asked to share your discoveries!
 +
 +
* How can the serial speed of communication be increased so that the PC and the Arduino talk faster?  I have tried changing the speed in the sketch from 9600 to a higher frequency, such as 115200, but that doesn't seem to work.  The Java machine supporting the arduino crashes...
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
 +
<br />
 +
[[Category:CSC231]][[Category:Arduino]][[Category:C]]

Latest revision as of 12:36, 9 October 2010

Back to CSC231 Weekly Schedule


CSC 231 Lab # 6

© D. Thiebaut, 2008

In this lab we are going to make the Arduino keep on listening to commands it gets from the assembly language on the PC, and execute each command as it receives them.

Getting the SimpleMessageSystem Library for the Arduino

ArduinoCGI.jpg

You need a new library for the Arduino called SimpleMessageSystem (Note: there is a newer version of it, Messenger, but I haven't been able to play with it enough to make it the version to use for the project.)

  • Boot the Ubuntu CD-Rom
  • Open a Terminal window and type
  sudo su
  cd 
  cd arduino-0010/hardware/libraries
  • download the library file from the Arduino Playground:
  wget http://www.arduino.cc/playground/uploads/Code/SimpleMessageSystem.zip  
  • unzip the file
  unzip SimpleMessageSystem.zip  
  • Remove the archive file
  rm SimpleMessageSystem.zip  
  • Set the ownership of the new library the same ownership as that of the arduino software
  chown -R 1000:1000 SimpleMessageSystem
  • Open the arduino GUI
  cd ../..
  .arduino
  • Create this new sketch.
/* 
 ---- SimpleMessageSystem Example 1 ----
 Control Arduino board functions with the following messages:
 
 r a -> read analog pins
 r d -> read digital pins
 w d [pin] [value] -> write digital pin
 w a [pin] [value] -> write analog pin 
 
 Base: Thomas Ouellet Fredericks 
 Additions: Alexandre Quessy  
 */

// Include de SimpleMessageSystem library
// REMOVE THE FOLLOWING LINE IF USING WIRING
#include <SimpleMessageSystem.h> 

void setup() {
  // The following command initiates the serial port at 9600 baud. Please note this is VERY SLOW!!!!!! 
  // I suggest you use higher speeds in your own code. You can go up to 115200 with the USB version, that's 12x faster
  Serial.begin(9600); //Baud set at 9600 for compatibility, CHANGE!
}

void loop() {
  if (messageBuild() > 0) { // Checks to see if the message is complete and erases any previous messages
    switch (messageGetChar()) { // Gets the first word as a character
    case 'r': // Read pins (analog or digital)
      readpins(); // Call the readpins function
      break; // Break from the switch
    case 'w': // Write pin
      writepin(); // Call the writepin function
    }
  }
}

void readpins(){ // Read pins (analog or digital)
  switch (messageGetChar()) { // Gets the next word as a character
    case 'd': // READ digital pins
      messageSendChar('d');  // Echo what is being read
      for (char i=2;i<14;i++) {
         messageSendInt(digitalRead(i)); // Read pins 2 to 13
      }
      messageEnd(); // Terminate the message being sent
      break; // Break from the switch
  case 'a': // READ analog pins
      messageSendChar('a');  // Echo what is being read
      for (char i=0;i<6;i++) {
        messageSendInt(analogRead(i)); // Read pins 0 to 5
      }
      messageEnd(); // Terminate the message being sent
   }
}

void writepin() { // Write pin
  int pin;
  int state;

  switch (messageGetChar()) { // Gets the next word as a character
  case 'a' : // WRITE an analog pin
    pin = messageGetInt(); // Gets the next word as an integer
    state = messageGetInt(); // Gets the next word as an integer
    pinMode(pin, OUTPUT); //Sets the state of the pin to an output
    analogWrite(pin, state); //Sets the PWM of the pin 
    break;  // Break from the switch
    
  case 'd' : // WRITE a digital pin
    pin = messageGetInt();  // Gets the next word as an integer
    state = messageGetInt();  // Gets the next word as an integer
    pinMode(pin,OUTPUT);  //Sets the state of the pin to an output
    digitalWrite(pin,state);  //Sets the state of the pin HIGH (1) or LOW (0)
  }
}


  • Save your sketch in the /root/sketches folder, and give it the name loop. I found that saving is not always straightforward, and it works best for me if I use the right arrow in the tab area of the Arduino GUI, click it, and use the rename option.
  • IMPORTANT
Note: When you boot your PC in Ubuntu mode from the live CD, you are working with a Linux computer that exists only on the CD and in the RAM. Whatever you create or copy to your ubuntu or root account exists only in RAM. As soon as you close the system, all your changes disappear. All the files you created disappear. For this reason, you will need a USB key to save all the files you create, including sketches and libraries, such as the Messenger library you created above.
Thus, at this point your /root directory should contain two folders: arduino-0010 and scketchbook, and sketchbook should have a new sketch called loop. You may want to drag the whole root folder to your USB memory stick and save all these new modifications.


CSC231 Ubuntu Root Folder.png

  • Use the Tools menu to set the board as the Diecimila, and the port as the USB port.
  • Compile. You may get warnings, but make sure you get a compile confirmation:
 Binary sketch size: 3402 bytes (of a 14336 byte maximum)
  • Upload to the Arduino. Check that the Receive and Transmit LED on the board light up to indicate data transfer.


UbuntuPinguin.png





Assembly Part on the PC

OUTPUTTING INFORMATION

LedOn

You are going to create a program called ledOn that turns on the LED connected to Pin 13.

  • Open a new Terminal window under Ubuntu and type
  sudo su
  cd
  • Highlight the contents of the file driver.c and copy it to the clipboard
  • In the terminal window type
  cat > driver.c
and then click the middle button of your mouse to paste the code you had highlighted
(You may want to copy these to your memory stick as well before you forget!!!)
  • Take a look at LedOn.asm. We'll look over the main features in class. It is important to note where the message containing the command is, and what its format is.
Note 2: Make sure the variable USB in ledOn.asm contains the same string identifying the USB port as reported in the Arduino GUI.
  • Assemble/compile the files:
 nasm -f elf -F stabs asm_io.asm
 nasm -f elf -F stabs ledOn.asm
 gcc -o ledOn driver.c ledOn.o asm_io.o
  • Run the program
  ./ledOn
Does the LED on Pin 13 turn ON? (If your Pin 13 is damaged from previous labs, please put a resistor plus LED connected to Pin 11).

LedOff

Ok, you figured it out. Make a copy of ledOn.asm under the name ledOff.asm, modify it, and compile it. Use it to turn OFF the LED.

Remember to check the Arduino Reference Page if you need to debug your sketch...

LedOnOff

Same scenario, but now this time make your assembly program send an ON message, followed by an OFF message in an endless loop.

Measure the frequency of the LED turning ON and OFF. This will give us an idea of how fast we can activate devices attached to the Arduino.

How could we make the LED blink faster, but still be able to control this blinking from the assembly program? If you have time, try to implement a solution.

For those interested creating sound, the human ear can hear sounds between about 300 and 20,000 Hz.


How to copy your environment back to the Ubuntu PC

When you want to resume work on your Arduino, you need to copy your environment back from the USB stick to the /root folder. If you try to drag and drop the folder from the USB stick to your /root folder, it will not work. This is because the desktop belongs by default to User Ubuntu, but you want to modify the folder of User Root, and Ubuntu won't allow you to do that.

The solution is to right-click on the /root folder, select property, and set its permissions to read/write for user, for group, and for others.

Then the drag and drop operation will work.

Open Problems

Things your instructor doesn't know, but that you might be able to discover while playing with the Arduino, and in which case you are asked to share your discoveries!

  • How can the serial speed of communication be increased so that the PC and the Arduino talk faster? I have tried changing the speed in the sketch from 9600 to a higher frequency, such as 115200, but that doesn't seem to work. The Java machine supporting the arduino crashes...