Problem uploading an example from the CMUcam library on Arduino Mega

Added by Anais LECOULTRE over 2 years ago

Hello Kwabena,

I have an Arduino Mega 2560 R3 and a black CMUcam4 module.
I tried the HAT and SAT programs from the library but they don't work.
When I upload them I get this error :
avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

I tried to upload a program to the Arduino (without the camera) like blinking a LED and it works.
My Arduino is connected to my computer via USB, and the CMUcam is placed as an Arduino shield (green and red lights are on, so it is powered).
I don't understand this error because some people have it for any program (on other forums) but I only have this problem with the CMUcam library's programs.

Could you help me? Could it be a serial connection problem? I've read on this forum that there are problems with Uno but not with Mega...
Do I need to upload the program on the Arduino without the camera, and when finished, place the camera as a shield? It makes no error, but then when I open the serial monitor I only see weird caracters like ΨÖCH@÷¸ísȘíÖ9Ä Œþc¸Î...

Thanks for your help!
Anais


Replies (9)

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Kwabena Agyeman over 2 years ago

Hi,

Sorry for the late response. The automatic email system which notifies me of forum posts was down...

You can't program your Arduino when the CMUcam4 is on top of it. You need to program your Arduino first and then put the Arduino on top of it. The CMUcam4 is best used with the Arduino Mega since it has multiple serial ports. Note: those test programs you tried only work on the Arduino mega.

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Anais LECOULTRE over 2 years ago

Thanks for the answer. The timeout with the programmer was another problem that I solved.

So now I can upload the Hardware Acceptance Test for example, and it works. When it is finished uploading, I press reset to wake up the camera.
On the Arduino serial monitor, I only see characters like "ΨvCH@÷¸ísȘíÖS& Œþc¸Î" and not "Hardware Acceptance Test Initializing..." like it should.
Do you know why?

I tried to test the communication between the camera and the Arduino on Serial1 and the serial monitor on Serial. It does not work either, cam.begin returns -9 (serial timeout).
I used the code below:


// Test serial connection between arduino and camera

#include <CMUcam4.h> // include CMUcam4 library
#include <CMUcom4.h>

CMUcam4 cam(CMUCOM4_SERIAL1); // configure serial port

void setup() {
Serial.begin(9600); // activate the serial connection at 9600 baud rate (serial monitor)
cam.begin(); // activates interface library
cam.LEDOn(5);
delay(5000);
cam.autoGainControl(false);
cam.autoWhiteBalance(false);
cam.LEDOn(CMUCAM4_LED_ON);

Serial.print("begin successful: ");
Serial.println(cam.begin()); // prints 0 if successful
}

void loop() {
if(cam.getButtonPressed() == true) // wait for user button to be pressed {
Serial.println("user button"); // print message on serial monitor
}
}


It prints begin succesful: -9. It never goes in the loop().

Thanks for trying to help me!

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Kwabena Agyeman over 2 years ago

Again, you can't run those test using an Arduino Uno. You need to connect the CMUcam4 via serial ports 1/2/3. Do not mount the camera on the Arduino as a shield. Run power and serial wires to it. I think you're having issues because you are trying to use it as a shield which does not work.

The TXO pin the CMUcam4 means TX OUT.
The RXI pin the CMUcam4 means RX In.

Also, use the 'pre' tags to post your code.

Anyway,

Run this code:

#include <CMUcam4.h> // include CMUcam4 library
#include <CMUcom4.h>

CMUcam4 cam(CMUCOM4_SERIAL1); // configure serial port

void setup()
{
  Serial.begin(9600); // activate the serial connection at 9600 baud rate (serial monitor)
  Serial.print("begin successful: ");
  Serial.println(cam.begin()); // prints 0 if successful
  cam.LEDOn(5);
  delay(5000);
  cam.autoGainControl(false);
  cam.autoWhiteBalance(false);
  cam.LEDOn(CMUCAM4_LED_ON); 
}

void loop()
{
  if(cam.getButtonPressed() == true) // wait for user button to be pressed
  {
    Serial.println("user button"); // print message on serial monitor
  }
}

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Anais LECOULTRE over 2 years ago

Sorry for the tags, I tried to do it with the 'code' button, but it didn't work. I'll use the 'pre' tag to post next time ;)

Like I said in my first post, I have an Arduino Mega, which is why I'm trying to run these tests. I didn't know it doesn't work with the camera as a shield, I thought it was designed as a shield to connect it like that... I read all the documentation on the camera and saw nowhere that it was written.

Anyway, I'll test the code tomorrow and wire the camera without using the shield, thanks! But I have a question: you deleted the cam.begin() instruction, will it work like that? There is just the print(cam.begin()) left. Will that instruction execute cam.begin() too? Or just write the result?

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Kwabena Agyeman over 2 years ago

Oh, if you have a Mega then the tests should work. However, you still can't use the CMUcam4 as a shield.

The shield design was a bad idea on my part. I was trying to make the device easier to use and ended up making it harder to. The problem being that most people don't understand the implication that if you use the the camera as a shield you can't print debug statements anymore to the serial terminal since the camera uses up that serial port.

Execution of cam.begin() inits the camera and returns the result. You should only do it once...

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Anais LECOULTRE about 2 years ago

Hi!

I used wires to connect the camera and the arduino, it works fine! Thanks!

But now, I have another problem... when I run a modified programm of line tracking, the function cam.getTypeTDataPacket(&data) and cam.getTypeFDataPacket(&bimage) return both -9... what is the problem here? And even if this function failed, it returns a value of data.mx and values for centroids: are they trusted values or not?

I also find weird that data.mx returns a totally different value than the centroids, even if it should be approximately the same (data.mx around 80 for a line in the center of the image, so it seems correct. but centroids value are variable, between 20 and 50)

I also tested DemoLineTrackingTemplate, but when printing the return value of the two functions, it is still -9... If you have an idea why I get this error, please tell me!

Thanks!!


// Initialisation
#include <CMUcam4.h>
#include <CMUcom4.h>

// Track white color
#define RED_MIN 230
#define RED_MAX 255
#define GREEN_MIN 230
#define GREEN_MAX 255
#define BLUE_MIN 230
#define BLUE_MAX 255

#define LED_BLINK 5 // 5 Hz
#define WAIT_TIME 5000 // 5 seconds

#define X_SAMPLE_SPACING 1 // Sample every 1 pixels.
#define Y_SAMPLE_SPACING 10 // Sample every 10 rows.

#define POLL_MODE false // Poll Mode: send one packet after calling trackColor instead of a stream of packets
#define YUV_MODE false

#define NOISE_FILTER 2

#define PIXELS_THRESHOLD 1
#define CONFIDENCE_THRESHOLD 50

CMUcam4 cam(CMUCOM4_SERIAL1); // open the serial communication on serial port 1

void setup()
{
  Serial.begin(9600); // activate the serial connection at 9600 baud rate (serial monitor)
  Serial.print("cam begin (0 if successful): ");
  Serial.println(cam.begin()); // initializes the camera and interface library

  cam.LEDOn(LED_BLINK); // auxiliary LED on, at blink frequency (here 5Hz)
  delay(WAIT_TIME); // wait for auto gain and auto white balance to run

  cam.autoGainControl(false); // turn auto gain off
  cam.autoWhiteBalance(false); // turn auto white balance off

  cam.LEDOn(CMUCAM4_LED_ON);

  cam.pollMode(POLL_MODE);
  cam.colorTracking(YUV_MODE); // better color tracking in YUV mode

  cam.noiseFilter(NOISE_FILTER);

  Serial.println("init done, waiting for user button to be pressed");
}

void loop()
{
  // Warning: The stack may overflow on non-Mega Arduino boards!

  CMUcam4_tracking_data_t data; // 16 bytes...
  CMUcam4_image_data_t bimage; // 600 bytes!!!

  cam.lineMode(true); // Line Mode: send type F data packets after sending type H/S/T data packets

  #if !POLL_MODE // sends a stream of packets
  cam.trackColor(RED_MIN, RED_MAX, GREEN_MIN, GREEN_MAX, BLUE_MIN, BLUE_MAX); // track white color
  #endif

  if(cam.getButtonPressed() == true) // when user button is pressed, take a picture and process it
  {
    #if POLL_MODE // sends only one packet
    cam.trackColor(RED_MIN, RED_MAX, GREEN_MIN, GREEN_MAX, BLUE_MIN, BLUE_MAX); 
    #endif

    Serial.print("get a type T data packet: ");
    Serial.println(cam.getTypeTDataPacket(&data)); // Get a tracking packet: [mx my x1 x2 y1 y2 pixels confidence]
    Serial.print("data mx: ");
    Serial.println(data.mx);
    Serial.print("get a type F data packet: ");
    Serial.println(cam.getTypeFDataPacket(&bimage)); // Get an image packet: binary bitmap 80x60 pixels (1 represents a tracked pixel, 0 an untracked pixel)

    // If poll mode is enabled then you have all the time in the world here...
    // If poll mode is disabled then you have 33.33 ms here...
    // Process the packet data safely here.
    // For example... below is a simple proportional controlled servo driver.

    long weightedCount = 0;
    long weightedAverage = 0;

    for(int y = (Y_SAMPLE_SPACING - 1); y < CMUCAM4_BINARY_V_RES; y += Y_SAMPLE_SPACING) // take an horizontal line in the picture every 10 rows and calculate the centroid
    {
      // Compute the average x centroid of a line.

      int count = 0;
      int average = 0;

      for(int x = 0; x < CMUCAM4_BINARY_H_RES; x += X_SAMPLE_SPACING) // take all the pixels on an horizontal line and see if they are white
      {
        if(cam.getPixel(&bimage, y, x))
        {
          count += 1;    // calculate how many pixels are white
          average += x;  // calculate the average x position of the white pixels
        }
      }

      Serial.print("centroid (between 0 and 159): ");
      Serial.println(average /= count); // Centroid of a line: sum of the average x position divided by the number of white pixels

      weightedCount += y; // Accumulate denominator: for each horizontal line, sum the y position
      weightedAverage += y * average; // Multiply in line weights: multiply the centroid of the x position by its y position ----- ! TO CHANGE !
    }

    Serial.print("weighted centroid (between 0 and 159): ");
    Serial.println(weightedAverage /= weightedCount); // Centroid of weighted average of lines: sum of the weighted average x position divided by the sum of y positions

    Serial.print("pixels: ");
    Serial.println(data.pixels);
    Serial.print("confidence: ");
    Serial.println(data.confidence);

    if((data.pixels >= PIXELS_THRESHOLD) && (data.confidence >= CONFIDENCE_THRESHOLD))
    {
      Serial.println("motor loop");

      int error = (((CMUCAM4_BINARY_H_RES / 2) - 1) - weightedAverage); // difference between the center of the image and the x position of the line (centroid)

      if (error>0){
        // light LED 1 // turn left (because of the definition of the error!)
        Serial.println("turn left");
      } else {
        // light LED 2 // turn right
        Serial.println("turn right");
      }
    } else {
      Serial.println("no motor loop");
    }
  }

  // Do something else here.
}

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Kwabena Agyeman about 2 years ago

-9 means the camera was not detected. The results of all your function calls are invalid, make sure begin() returns 0 before continuing. You've likely connected the CMUcam4 to Serial1 incorrectly. The serial wires may be reversed or the power/ground is wrong.

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Anais LECOULTRE about 2 years ago

I don't understand why the camera is not detected, as cam.begin returns 0 in the init loop... Do you have any idea how I could solve this?
The power/ground wires are rightly connected, and the camera has the red and green LEDs on.

RE: Problem uploading an example from the CMUcam library on Arduino Mega - Added by Kwabena Agyeman about 2 years ago

It's the serial connection. Make sure that you've connected the serial pins on the camera correctly.

(1-9/9)