Arduino to Matlab – Read in sensor data!

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

MANOI in 2010 and beyond +

Thursday, January 7th, 2010

2009 was a pretty good year in terms of getting MANOI to do stuff! I figured out how to make MANOI write, walk, and dance around and be on TV. I have a pretty good template for programming MANOI too, and it seems quite rugged. 2009 also gave me the chance to learn the limitations of this robot, that being the knee servos.

I was always under the impression that “great” humanoid robots can walk, run, and kick very well, basically being the best robots in RoboCup. MANOI will never be a RoboCup robot, since the knees are too weak and the stronger knees cost $300 a piece. I realized this a few months ago, admitting it is sort of harder ;)

But, MANOI can be an interactive robot and do funny things… so in 2010, I’m going to focus more on the appearance and movements of MANOI. I want it so that the person interacting with MANOI can become attached to it, feeling that it exhibits an actual persona, instead of a programmed robot. I really feel that this was evident in MANOI’s Holiday Xtravaganza, but it still needs some improvements.

The first few steps would be to decorate MANOI’s head with two ears and two eyebrows. I’m considering antennae instead of eyebrows, because I don’t really want MANOI to come off as an uncanny human, but rather a friendly alien. I already have the two servos needed for the ears, so I’ll probably do those first. :D

Getting various sensor data will also be key in making MANOI more sociable. Currently there are three sensors on MANOI- the IR sensor, the gyro, and a LDR which is used for shaking hands. I’m thinking that those sensors alone are going to useful enough, for the time being.

It might be cool to have MANOI communicate back to a Mac so that the computer could produce audible results (like my Robbie the Robot science fair project).

There’s lots of possibilities out there, so this is pretty much a blog post of my scrambled thoughts. :P 2010 will be interesting, for sure!





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

MANOI’s Holiday Xtravaganza

Thursday, December 24th, 2009


HAPPY HOLIDAYS TO EVERYONE!!! :)

MANOI Holiday Xtravaganza

MANOI Holiday Xtravaganza

MANOI Holiday Xtravaganza

MANOI Holiday Xtravaganza

MANOI Holiday Xtravaganza

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

MANOI on Discovery Channel – Daily Planet!

Friday, December 18th, 2009

RobotGrrl on Daily Planet

On December 17th, MANOI made its TV-screen ‘debut’ to gazillions of viewers in Canada! Daily Planet, a news style of a show for DIY’ers and scientists, featured MANOI’s holiday Xtravaganza warmup in the PlanetYou clip!

You can see the clip online by clicking here. It’s after the iPhone App part. ^_^

It was pretty awesome to see MANOI on TV, and a great way to ring out the year. WOOT! :D

The actual version of MANOI’s Holiday Xtravaganza will be posted soon :)

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

Friday Night Robotics * 3 – MANOI’s Holiday Xtravaganza Warmup

Monday, December 14th, 2009

This weekend really gives patronage to how challenging the sport of robotics is (Technically it’s not a sport since it’s not in the Olympics), and how frustrating everything can be!!!! >:(

Last year, MANOI barely showed off any holiday spirit. Just a seemingly 2D stocking sticky-tacked onto its head.

This year I really wanted to improve on that because… well, I don’t really have a reason. I just wanted to. I wanted to see what it would be like for MANOI to ring bells ^_^

The decorating started off with just a hat, followed by the hunt to find jingle bells that are a circle, not a bell. My mom eventually figured out the type of bell that I was looking for, and bought me some festive bells and bows!

Et voila, MANOI! C’est magnifique! (Tada, MANOI is magnificent!)

Festive MANOI

I spent an enormous amount of time getting various actions set up so that I can string them together into a final product. In total, there are 7 different movements. My favourite of which, called ‘tango’ can be seen in the video below where one of MANOI’s arms is outwards, and another is inwards, perpendicular to its front shell with the bow. Surprisingly enough, its only two motion frames:

  1. int tangoHeadTilt = 400;
  2. int tangoHandTilt = 200;
  3. int tangoHandIn = 120;
  4. int tangoHandUp = 500;
  5. int tangoArmUp = 500;
  6. int tangoArmTilt = 600;
  7. int tangoHipTilt = 200;
  8. int leanBack = -25;
  9.  
  10. int tango_keyframe1[17] = {
  11.       HOME0-tangoHeadTilt,
  12.       HOME1+tangoHandTilt,
  13.       HOME2-tangoHandIn,
  14.       HOME3-tangoHandUp,
  15.       HOME4,
  16.       HOME5+tangoArmUp,
  17.       HOME6+tangoArmTilt,
  18.       HOME16+tangoHipTilt,
  19.       HOME17+leanBack,
  20.       HOME18,
  21.       HOME19,
  22.       HOME20,
  23.       HOME21-tangoHipTilt,
  24.       HOME22+leanBack,
  25.       HOME23,
  26.       HOME24,
  27.       HOME25
  28. };
  29.  
  30. int tango_keyframe2[17] = {
  31.       HOME0+tangoHeadTilt,
  32.       HOME1,
  33.       HOME2+tangoArmUp,
  34.       HOME3+tangoArmTilt,
  35.       HOME4+tangoHandTilt,
  36.       HOME5-tangoHandIn,
  37.       HOME6+tangoHandUp,
  38.       HOME16-tangoHipTilt,
  39.       HOME17+leanBack,
  40.       HOME18,
  41.       HOME19,
  42.       HOME20,
  43.       HOME21+tangoHipTilt,
  44.       HOME22+leanBack,
  45.       HOME23,
  46.       HOME24,
  47.       HOME25
  48. };
  49.  
  50. void tango(int repeat) {
  51.   for(int i=0; i<repeat; i++) {
  52.   setFrame(tango_keyframe1, 800, 50);
  53.   setFrame(tango_keyframe2, 800, 50);
  54.   }
  55. }

It’s weird because tango looks way more complicated than a movement that has 6 frames, the ‘both arm jingle’. This is where MANOI jingles both of its arms, so one is back and the other is forwards, then the hands both jingle.

  1. int bothArmJingle_frame1[17] = {
  2.       HOME0+200,
  3.       HOME1,
  4.       HOME2+leftArmOut,
  5.       HOME3+leftArmForward,
  6.       HOME4,
  7.       HOME5+rightArmOut,
  8.       HOME6+rightArmForward,
  9.       HOME16,
  10.       HOME17,
  11.       HOME18,
  12.       HOME19,
  13.       HOME20,
  14.       HOME21,
  15.       HOME22,
  16.       HOME23,
  17.       HOME24,
  18.       HOME25
  19. };
  20.    
  21. int bothArmJingle_frame2[17] = {
  22.       HOME0+200,
  23.       HOME1-leftWristMovement,
  24.       HOME2+leftArmOut,
  25.       HOME3+leftArmForward+50,
  26.       HOME4-rightWristMovement,
  27.       HOME5+rightArmOut,
  28.       HOME6+(rightArmForward-170),
  29.       HOME16,
  30.       HOME17,
  31.       HOME18,
  32.       HOME19,
  33.       HOME20,
  34.       HOME21,
  35.       HOME22,
  36.       HOME23,
  37.       HOME24,
  38.       HOME25
  39. };
  40.  
  41. int bothArmJingle_frame3[17] = {
  42.       HOME0+200,
  43.       HOME1+leftWristMovement,
  44.       HOME2+leftArmOut,
  45.       HOME3+leftArmForward,
  46.       HOME4+rightWristMovement,
  47.       HOME5+rightArmOut,
  48.       HOME6+rightArmForward,
  49.       HOME16,
  50.       HOME17,
  51.       HOME18,
  52.       HOME19,
  53.       HOME20,
  54.       HOME21,
  55.       HOME22,
  56.       HOME23,
  57.       HOME24,
  58.       HOME25
  59. };
  60.  
  61. int bothArmJingle_frame4[17] = {
  62.       HOME0-200,
  63.       HOME1,
  64.       HOME2+leftArmOut,
  65.       HOME3-leftArmForward,
  66.       HOME4,
  67.       HOME5+rightArmOut,
  68.       HOME6-rightArmForward,
  69.       HOME16,
  70.       HOME17,
  71.       HOME18,
  72.       HOME19,
  73.       HOME20,
  74.       HOME21,
  75.       HOME22,
  76.       HOME23,
  77.       HOME24,
  78.       HOME25
  79. };
  80.  
  81. int bothArmJingle_frame5[17] = {
  82.       HOME0-200,
  83.       HOME1-leftWristMovement,
  84.       HOME2+leftArmOut,
  85.       HOME3-(leftArmForward+50),
  86.       HOME4-rightWristMovement,
  87.       HOME5+rightArmOut,
  88.       HOME6-(rightArmForward+50),
  89.       HOME16,
  90.       HOME17,
  91.       HOME18,
  92.       HOME19,
  93.       HOME20,
  94.       HOME21,
  95.       HOME22,
  96.       HOME23,
  97.       HOME24,
  98.       HOME25
  99. };
  100.  
  101. int bothArmJingle_frame6[17] = {
  102.       HOME0-200,
  103.       HOME1+leftWristMovement,
  104.       HOME2+leftArmOut,
  105.       HOME3-leftArmForward,
  106.       HOME4+rightWristMovement,
  107.       HOME5+rightArmOut,
  108.       HOME6-rightArmForward,
  109.       HOME16,
  110.       HOME17,
  111.       HOME18,
  112.       HOME19,
  113.       HOME20,
  114.       HOME21,
  115.       HOME22,
  116.       HOME23,
  117.       HOME24,
  118.       HOME25
  119. };
  120.  
  121.  
  122. void bothArmJingle(int repeat) {
  123.  
  124.   for(int i=0; i<repeat; i++) {
  125.    
  126.   setFrame(bothArmJingle_frame1, 500, 0);
  127.   for(int i=0; i<5; i++) {
  128.   setFrame(bothArmJingle_frame2, 20, 0);
  129.   setFrame(bothArmJingle_frame3, 20, 0);
  130.   }
  131.   setFrame(bothArmJingle_frame4, 500, 0);
  132.   for(int i=0; i<5; i++) {
  133.   setFrame(bothArmJingle_frame5, 20, 0);
  134.   setFrame(bothArmJingle_frame6, 20, 0);
  135.   }
  136.  
  137.   }
  138.  
  139. }

Crazy right? I’ve found that in most of my other MANOI motions, the movement was very focused, with no other moving parts. So I made sure to make the most things move at once. :D I think it gives off a more exciting vibe. ^_^ Check out the video!

It really wasn’t as easy as the video makes it look. There were a lot of times where various movements wouldn’t match up, so they would catch the balance off-guard. Also, one of MANOI’s ankle servos went crazy on me, so it let the other foot down, causing a fall.

It’s super challenging to fix this because when MANOI falls, there are at least two things that temporarily break. You fix those things, but while you’re fixing them, other things break. While you’re trying to fix the new things, you can’t get a good grip on the robot because its entire body is made up of servos, and if you set the robot down, another thing will break. I would be willing to wager that working in no-gravity is a cinch compared to this!

Once everything is working again, you go and fix the motion. Fixing the motion is a whole separate routine of trial and error- you change some of the values of the servos and see what happens. There are two options for the next step, either have super fast reflexes, or only program with one hand on the keyboard. Since macs are not very one hand friendly, I have to have super fast reflexes to turn the power switch off on MANOI so that it won’t fall. If it does fall, you have to go back and fix everything.

In any case, once the motion is fixed, you go back and try it with the other movements. Just your luck, the battery runs out! MANOI falls, and you have to fix everything again.

It’s so unbelievably frustrating!! >:( Especially doing it for 3 days straight! There have been three times where MANOI has fallen off the table, about 1 meter (3 feet) tall. The risk of this is MANOI getting shocked by me, the air, and the carpet! It’s really nerve wracking, but thankfully MANOI is alright. Here’s a video of all of MANOI’s falls from the various videos that I tried to take:

At the end of the day, I really enjoy seeing MANOI move around. I think of it this way: it is my frustration and patience that has given an otherwise inanimate object LIFE. How cool is that? :)

There will be more videos of MANOI in its holiday gear showing up soon, with more blinky lights too!

Festive MANOI

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