How To: Display Images on Your NXT using RobotC (JPEG, PNG, etc.)

18 02 2012

First of all, let me say that people have found various ways to do this, and that this might not be the easiest way. A big benefit, though, is that the image data you end up getting is very easily accessible and manipulable. Also, this works with *a lot* of image formats, including JPEG, JPG, PNG, BMP, and others. Anyhow, let’s get started, shall we?

What you need

  • A MindStorms NXT intelligent brick
  • RobotC
  • Paint.NET, a free image creation/ editing software which you’re going to use to create your image, with this plugin [LINK] installed. Don’t know how to install Paint.NET plugins? Refer to this page here [LINK].

Step 1: Creating or Importing your image

For creating your own image: open Paint.NET, click the “Image” menu, and then open the “Canvas size…” option. In the wizard, specify any image size, up to 100 pixels wide and 64 pixels high. Then use the pencil or any of the other tools to create your image.

For importing an image: open Paint.NET, click “open,” navigate to the desired image, and double click it. Then, go to the “Image” menu, open the “Image size…” option, and, in the wizard, specify any image size, up to 100 pixels wide and 65 pixels high.

Step 2: Making your image black and white

First, open the “Adjustments” menu and click “Black and White.” Then, again in the “Adjustments” menu, open “Brightness/ Contrast,” and, in the wizard that pops up, set the contrast to 100.

You can play around with the brightness to change how dark a color needs to be to become black, and how light a color needs to be to end up white. Once you’re satisfied with your results, hit the “OK” button and your image is ready for the next step.

Step 3: Saving your image

Open the “File” menu and click “Save as…”. You can save your image under any name, anywhere, as long as you make sure you select “Portable Bit Map Files (*.pbm)” from the “Save as Type” drop down menu. If this option is not available, you didn’t install the plugin right, so re-install it and try again. Click image to enlarge.

Step 4: Reformatting your image’s code

Next up, you need to turn the image you just created into something the NXT can actually use. Go to the folder you saved your image in, right click the picture, and hit “Edit with Notepad++”. In the code you end up with, remove everything before the 1′s and 0′s (usually the top two lines).

Then, hit ctrl and f on your keyboard to open the “Find” wizard. The next steps are very important, so pay close attention:

  1. In the wizard, go to the second tab, which should say “Replace”
  2. In the “find what” box, type ” ” [hit the space bar once], and in the “Replace with” box, type nothing. Hit “Replace all.”
  3. In the “find what” box, type “1″, and in the “Replace with” box, type “1,”. Hit “Replace all.”
  4. In the “find what” box, type “0″, and in the “Replace with” box, type “0,”. Hit “Replace all.”
  5. Finally, turn the code into a single line by going to the end of every line of code and hitting delete

Step 5: Displaying the image on your NXT

First of all, copy this code into your program somewhere:

int i, j;
int image[HEIGHT][WIDTH] = {PUT 1'S AND 0'S HERE};
for(i=0; i<HEIGHT; i++){
  for(j=0; j<WIDTH; j++){
    if(image[i][j] == 0){
      nxtSetPixel(j,-i+HEIGHT-1);
    }
  }
}

Replace “WIDTH” with the width of your image, “HEIGHT” with the height of your image, and “PUT 1′S AND 0′S HERE” with your image code (copy and paste it between the curly brackets).

And there you have it: you can now display any image you want on your NXT screen using RobotC and some freeware :)





How To: make Custom NXT – PF Cables

26 03 2011

This guide will tell you how to create your own Lego Mindstorms NXT to Lego Power Functions cable, to control any PF motor or light, how to program it, and what you can do with it.

What you’ll need:

  • An expendable regular NXT cable
  • An expendable PF (could be an extension cord, light, motor of XL motor)
  • Soldering equipment
  • Insulation wire
  • Some very basic electricity knowledge
  • If under 18: permission from parent/ guardian to solder, and cut your NXT/ PF wires

Steps

  1. Get a good workspace set up; make sure you’re either outside or keep some windows open (solder is toxic); also lay something on your table to catch falling solder.
  2. Cut the NXT and PF cables in half, keeping about 5 cm (2 inches) of wire on both.
  3. Strip both wires down to the copper; for the NXT cable, cut off the blue, yellow, green and red wires; strip the black and white onesNXT to PF illustration 1
  4. Connect one of the middle two PF wires to the white NXT wire (it doesn’t really matter which one; if you’re making multiple cables, though, keep consistent), and connect the other one to the black wire.NXT to PF illustration 2
  5. Solder these connections and individually insulate them so they’ll never be able to touch each other
  6. Twist the wires together and insulate the whole thing to complete your cable, you can heat shrink it if you have the means to.NXT to PF illustration 4

Programming

Since both the Lego power functions and Mindstorms NXT work on 9v (6 AA batteries), programming them together isn’t very difficult. All you need to do is use a regular motor function (or motor block in NXT-G) to send power through one of the three possible ports (this won’t work with sensor input ports). One very important thing, though, is to turn off the built in motor PID control, because the program doesn’t get any feedback from the PF it’s controlling. So, it’ll always turn the power to 100 to compromise the speed it doesn’t think it has.

So this (RobotC code):

task main(){
  int waitMSecs = 50;
  nMotorPIDSpeedCtrl[motorA] = mtrNoReg;
  nMotorPIDSpeedCtrl[motorB] = mtrNoReg;
  nMotorPIDSpeedCtrl[motorC] = mtrNoReg;
  motor[motorB] = 25;
  motor[motorC] = 50;
  while(true){
    motor[motorA] = 0;
    wait1Msec(waitMSecs);
    motor[motorA] = 100;
    wait1Msec(waitMSecs);
  }
}

would make the PF function connected to…

  • Port A: flicker on and off every 50 mSecs
  • Port B: turn on to 25% of the maximum power
  • Port C: turn on to 50% of the maximum power

So, now you’re able to connect ANY Lego power function straight to you’re NXT, and, without any difficult programming, control it straight from there very accurately, with the possibility to use timers, loops, reverse power, dimming and much more!

Links:








Follow

Get every new post delivered to your Inbox.

Join 34 other followers