Posts Tagged ‘Arduino’

Arduino to Matlab – Read in sensor data!

Posted by Erin, the RobotGrrl on Friday, January 15th, 2010

Matlab is by far the best software I have ever seen when it comes to plotting data and showing it in a visual format. I figured it would be awesome if I could get Arduino and Matlab to work together!

Here’s the code that I came up with that works reasonably fast. It doesn’t wait for the buffer to be filled to then store it to a variable. Here, it is asynchronous communication. ^_^

This is a basic screenshot of what you will get, minus the green stars.

matlabscreenshot

Here is the code. Let me know if you use, it would be neat to see what everyone would come up with!
*Note: The code highlighting for Matlab was buggy, so the below is using C code highlighting. Once you paste it into Matlab, it will be fine.

  1. % Basic Arduino and Matlab
  2. % Communication with an external hardware device
  3. % ———————————————-
  4. %
  5. % Erin Kennedy
  6. % Jan. 18, 2010
  7. %
  8.  
  9.  
  10. clear all; clc; close all;
  11.  
  12.  
  13. % Try-catch is to prevent Matlab from crashing when the program is finished
  14. try
  15.  
  16.    
  17. % Initialize serial port
  18. s = serial(‘/dev/tty.usbserial-A4001lVG’);
  19. %set(s, ‘ Terminator’, ‘LF’); % Default terminator is \n
  20. set(s,‘BaudRate’, 9600);
  21. set(s,‘DataBits’, 8);
  22. set(s,‘StopBits’, 1);
  23. fopen(s);
  24. s.ReadAsyncMode = ‘continuous’;
  25.  
  26.  
  27. % Various variables
  28. numberOfDatas = 50;
  29. data = zeros(1, numberOfDatas);
  30. i = 1;
  31.  
  32.  
  33. % Main graph figure
  34. figure(1);
  35. hold on;
  36. title(‘Incomming Data from External Device’);
  37. xlabel(‘Data Number’);
  38. ylabel(‘Analog Voltage (0-1023)’);
  39.  
  40.  
  41. % Start asynchronous reading
  42. readasync(s);
  43.  
  44.  
  45. while(i<=numberOfDatas)  
  46.    
  47.    
  48.     % Get the data from the serial object
  49.     data(i) = fscanf(s, ‘%d’);
  50.    
  51.     % Plot the data
  52.     figure(1);
  53.     plot(i, data(i), ‘m*’);
  54.    
  55.     % Ensure there are always 10 tick marks on the graph
  56.     if(i>10)
  57.        xlim([i-10 i]);
  58.        set(gca,‘xtick’,[i-10 i-9 i-8 i-7 i-6 i-5 i-4 i-3 i-2 i-1 i])
  59.     end
  60.    
  61.     % Draw and flush
  62.     drawnow;
  63.    
  64.     %Increment the counter
  65.     i=i+1;
  66.    
  67.    
  68. end
  69.  
  70.  
  71. % Give the external device some time…
  72. pause(3);
  73.  
  74. return;
  75.  
  76. catch
  77.  
  78. % Some of these crash the program it depends. The serial port is left
  79. % open, which is not good.                                              
  80. stopasync(s);
  81. fclose(s); % bad
  82. %delete(s);
  83. %clear s;
  84.  
  85. fprintf(1, ‘Sorry, you"re going to have to close out of Matlab to close the serial port\n);
  86.  
  87. return
  88.  
  89. end

The code for the Arduino is this:

  1. //
  2. // BubbleBoy -> Matlab
  3. // ——————-
  4. //
  5. // Read LDR data, print them to Serial (where Matlab will receive them)
  6. //
  7.  
  8. int LDRpin = 0;
  9.  
  10. void setup() {
  11.  
  12.   Serial.begin(9600);
  13.   pinMode(LDRpin, INPUT);
  14.  
  15. }
  16.  
  17. void loop() {
  18.  
  19.  int photocellReading = analogRead(LDRpin);
  20.  
  21.  Serial.println(photocellReading, DEC);
  22.  delay(200);
  23.  
  24. }

Enjoy, happy matlabbing!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Posted in: Programming, Projects, School.

Friday Night Robotics – MusicBox!

Posted by Erin, the RobotGrrl on Sunday, October 11th, 2009

This Friday I had the most awesome idea of a weekend project ever! A 21st century MusicBox, using an Arduino and have blinking LEDs!

I had a Sparkfun box lying around, which is an ideal size for an Arduino and a WaveShield.

Friday Night Robotics - MusicBox!

I wanted to have LEDs outlining the box, so I went to work on it:

Friday Night Robotics - MusicBox!

Friday Night Robotics - MusicBox!

Just as a disclaimer- I designed it wrong, so the LEDs don’t work well. If you’re looking to follow my steps, DON’T DO IT! :P

Friday Night Robotics - MusicBox!

Friday Night Robotics - MusicBox!

Then, installing them into the box:

Friday Night Robotics - MusicBox!

It looks nice!

Friday Night Robotics - MusicBox!

Once all of the connections are made, it’s pretty tight in there:

Friday Night Robotics - MusicBox!

The problem though, is that I designed it so that all the LEDs are in series. Since al LEDs aren’t created equally, some suck up more power and therefore can’t share it with the others. This is what happened:

Friday Night Robotics - MusicBox!

It’s pretty sad! :( But, I think that I can fix it because I soldered the resistors together, not the actual LEDs together. :D I was too excited to start this project, so I didn’t bother to plan =) I guess planning would have been better, but it would also have been too boring.

I also worked on a NXT LED blinky thingy. There are these HiTechnic Protoboards that you can get, and basically you attach them to one of the sensor inputs, and you can control power to certain ports and such.

In this case, there’s 6 output pins that you can control. Sounds like an opportunity to use LEDs to me! :D

NXT + LEDs

This is what the setup looks like:

NXT + LEDs

It goes like this: NXT -> HiTechnic ProtoBoard Sensor Adapter -> 6 LEDs

In order to output instructions to the HiTechnic ProtoBoard, you need ‘drivers’, or headers. They’re located here.

Here is the code for the LEDs, in RobotC:

  1. #pragma config(Sensor, S1,     HTPB,                sensorI2CCustom9V)
  2. //*!!Code automatically generated by ‘ROBOTC’ configuration wizard               !!*//
  3.  
  4. /*
  5.   Crazy LEDs!
  6.   Erin K
  7.   Oct. 9th, 2009
  8. */
  9.  
  10. #include "drivers/common.h"
  11. #include "drivers/HTPB-driver.h"
  12.  
  13. byte theLEDs[] = { 0×01, 0×02, 0×04, 0×08, 0×10, 0×20 };
  14.  
  15. task main() {
  16.  
  17.   // Setup all the digital IO ports as outputs (0xFF)
  18.   if (!HTPBsetupIO(HTPB, 0xFF)) StopAllTasks();
  19.   wait1Msec(200);
  20.  
  21.   while(true) {
  22.  
  23.     // The delay time
  24.     int theTime = 50;
  25.  
  26.     // LEDs going up
  27.     for(int i=0; i<6; i++) {
  28.       if (!HTPBwriteIO(HTPB, theLEDs[i])) nxtDisplayTextLine(5, "ERR WRITE");
  29.       wait1Msec(theTime);
  30.     }
  31.  
  32.     // LEDs going down
  33.     for(int i=5; i>=0; i) {
  34.       if (!HTPBwriteIO(HTPB, theLEDs[i])) nxtDisplayTextLine(5, "ERR WRITE");
  35.       wait1Msec(theTime);
  36.     }
  37.  
  38.     alive();
  39.  
  40.   }
  41.  
  42. }

This is what the code does:

To wrap up this Friday Night Robotics, I checked out the Adafruit Ask an Engineer chat. It was pretty cool! I learnt about how LEDs work, and how much it costs to create a Teenyduino! Everyone should check it out, Saturday at 10:00PM EST.

The only things that I didn’t get to do that I wanted to was play with MANOI and the iRobot Create. I’m kinda worried that MANOI’s batteries are drying out as they haven’t been exercised in a while :S EEP!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Posted in: Art, Programming, Projects, Robot.

Friday Night Robotics – Hula Hooping Motion?

Posted by Erin, the RobotGrrl on Monday, August 24th, 2009

Building on last week’s RGB LEDs, it was time to add some motions to MANOI that would suit the carnival-ish theme.

Friday Night Robotics - MANOI Hula-hooping

A significant amount of time was spent on brainstorming ideas on how to make MANOI walk. The key will be the first step, as it has to get the rhythm started. The following steps will maintain the rhythm. Another point to look at would be shoes for MANOI. For walking, MANOI uses these ‘flip flops’ :

Friday Night Robotics - MANOI Hula-hooping

For skating, MANOI has roller skates that are made out of lego. These shoes are attached by velcro. It would be interesting to see if I could use some ShapeLock to make new shoes. (Shoes, Omg shoes. Shoes. Let’s buy some shoes!) By using ShapeLock, I may be able to eliminate some of the tilting that the velcro introduces, and thus eliminating another reason why the robot may fall.

In any case, I want to build on MANOI’s hip and leg motions like I did with the arm motions. I started with a simple swaying function, but Boom from the Robotics themed floor suggested an awesome suggestion- tilting the body to make it go forward and backward. In the right sequence, this would make a hula hoop motion!

It was interesting the way we did it. Instead of just telling the hip motors to tilt forward, we also told the shin motors to tilt backward. This maintained the center of balance. :D

Friday Night Robotics - MANOI Hula-hooping

We added onto this with arm motions. When MANOI tilts backwards, the arms go up. We tried keeping the arms up for tilting forward, but it shifted the center of balance too much.

Friday Night Robotics - MANOI Hula-hooping

Here are two videos of the motion:

There are more photos on Flickr!

I’m going to blog more about the Robotics themed floor later, as well as the Autonomous Robotics Club! =) I also adopted an iRobot Create recently, and I’m controlling it with an Arduino. It’s fun! :)

Oh yeah, and MANOI fell about 3 feet from a desk when its battery ran out. It survived just fine, and nothing was broken!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Posted in: MANOI, Projects, Robot.

Friday Night Robotics – Wild Thing

Posted by Erin, the RobotGrrl on Sunday, June 28th, 2009

This actually wasn’t on a Friday (Friday I was making an iPhone app), but rather Saturday. :D

Since the Sanguino requires too many female headers (which cost money, and I don’t have any), I decided to switch up to the Arduino MEGA. It fits in the box that is already there, so it is all good.

Originally I thought that the Wave Shield would work on the MEGA, but it turned out that it didn’t. What I ended up doing instead, though was using one of the MEGA’s extra Serial lines to send a command to another Arduino to start playing music (on the Wave Shield).

I used my iHome speaker thing that I bought at Walmart for $20. Pretty good value, and it’s purple.

Friday Night Robotics - Wild THing

It was pretty fun. However, I only used the motions that I created a while ago when MANOI’s knees were broken. A blog post is coming shortly about the previous FNR where I fixed the gears :D

I even attached a wild duck to MANOI’s head for the occasion:

Friday Night Robotics - Wild THing

Here is a video, it is not too good:

Friday Night Robotics – Wild Thing from RobotGrrl on Vimeo.

See, the problem in the video (when I had to shake it) was that the TX line to the SSC was loose. It does this from time to time. Especially after switching boards!

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Posted in: MANOI, Programming, Projects, Robot.

Update on MANOI’s code

Posted by Erin, the RobotGrrl on Monday, May 18th, 2009

I updated MANOI’s code a little bit. I think it will work better, it’s more organized for sure!

This program “lifts” MANOI’s broken leg.

  1. #include <stdio.h>
  2.  
  3. int HOME0 = 1800;
  4. int HOME1 = 1500;
  5. int HOME2 = 1000;
  6. int HOME3 = 1300;
  7. int HOME4 = 1300;
  8. int HOME5 = 1600;
  9. int HOME6 = 1900;
  10. int HOME16 = 1550;
  11. int HOME17 = 1200;
  12. int HOME18 = 900;
  13. int HOME19 = 1600;
  14. int HOME20 = 1600;
  15. int HOME21 = 1500;
  16. int HOME22 = 1200;
  17. int HOME23 = 1000;
  18. int HOME24 = 1580;
  19. int HOME25 = 1600;
  20.  
  21. void setup() {
  22.   Serial.begin(9600);
  23.  
  24.   int homeFrame[18] = {
  25.       HOME0,
  26.       HOME1,
  27.       HOME2,
  28.       HOME3,
  29.       HOME4,
  30.       HOME5,
  31.       HOME6,
  32.       HOME16,
  33.       HOME17,
  34.       HOME18,
  35.       HOME19,
  36.       HOME20,
  37.       HOME21,
  38.       HOME22,
  39.       HOME23,
  40.       HOME24,
  41.       HOME25
  42.     };
  43.  
  44.   setFrame(homeFrame, 100, 500);
  45.  
  46.   Serial.println("Hello world!");
  47. }
  48.  
  49. void loop() {
  50.   for(int i=0; i<500; i+=50) {
  51.   int liftLeftLeg[17] = {
  52.       HOME0,
  53.       HOME1,
  54.       HOME2,
  55.       HOME3,
  56.       HOME4,
  57.       HOME5,
  58.       HOME6,
  59.       HOME16,
  60.       HOME17+i,
  61.       HOME18,
  62.       HOME19,
  63.       HOME20,
  64.       HOME21,
  65.       HOME22,
  66.       HOME23,
  67.       HOME24,
  68.       HOME25
  69.     };
  70.     setFrame(liftLeftLeg, 500, 5000);
  71.   }
  72. }
  73.  
  74. void setFrame(int theFrame[], int moveTime, int delayTime) {
  75.   char str[250]; // I will count the actual number, some day
  76.   sprintf(str, "#0 P%d #1 P%d #2 P%d #3 P%d #4 P%d #5 P%d #6 P%d #16 P%d #17 P%d #18 P%d #19 P%d #20 P%d #21 P%d #22 P%d #23 P%d #24 P%d #25 P%d T%d", theFrame[0], theFrame[1], theFrame[2], theFrame[3], theFrame[4], theFrame[5], theFrame[6], theFrame[7], theFrame[8], theFrame[9], theFrame[10], theFrame[11], theFrame[12], theFrame[13], theFrame[14], theFrame[15], theFrame[16], moveTime);
  77.   Serial.println(str);
  78.   delay(moveTime + delayTime);
  79. }

I am more happier with this :) Pretty easy to program with one hand, and MANOI in the other.
Thanks to all who left a comment on the previous post! =)

Post to Twitter Post to Delicious Post to Digg Post to Facebook Post to Reddit Post to StumbleUpon

Posted in: MANOI, Programming, Projects, Robot.