Need help creating a wrapper class for an Arduino Robot

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
IAmSam
 
Posts: 5
Joined: Fri Sep 04, 2015 7:55 pm

Need help creating a wrapper class for an Arduino Robot

Post by IAmSam »

Hi,

I recently built myself a small 4 wheeled robot chassis powered by an Arduino Uno and an Adafruit MotorShield V2.3. I thought it'd be a good way to practice my programming over the holiday (I'm a CompSci student) and maybe learn a bit of C++ as all my current programming knowledge is in Java. Anyway, I assembled the robot and got it moving, so I'm fairly certain that none of the boards are broken. The next thing I tried to do was to create a wrapper class to contain the setup of the robot and some basic commands (such as forward, reverse and turn). I tried to do this by creating a new library (I followed this tutorial: https://www.arduino.cc/en/Hacking/LibraryTutorial, however this is where I am running into some problems.

First here is the code for my Robot.h file:

Code: Select all

#ifndef Robot_h
#define Robot_h

#include "Arduino.h";
#include <Wire.h>
#include <Adafruit_MotorShield.h>

class Robot
{
  public:
    Robot();
  private:
    Adafruit_MotorShield AFMS = Adafruit_MotorShield();
};

#endif
This is my Robot.cpp file:

Code: Select all

#include "Arduino.h"
#include "Robot.h"
#include <Wire.h>
#include <Adafruit_MotorShield.h>

Robot::Robot()
{
  Serial.begin(9600);
  Serial.println("Foo");
  AFMS.begin();
  Serial.println("Bar");
}
And finally is the code in my Main sketch:

Code: Select all

#include <Robot.h>
#include <Wire.h>
#include <Adafruit_MotorShield.h>

Robot myBot;

void setup()
{
  myBot = Robot();
}

void loop()
{
}
The code successfully uploads to the Arduino, but the only thing printed on the Serial monitor is "Fo". The entire program freezes after just two characters are printed. But if I delete the AFMS.begin() both of the statements print. I was wondering if anyone would be kind enough to help me out here? I understand that all of this can be done in the same sketch (and I have got the robot moving by coding in just one sketch), but if possible I would much rather separate it out.

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by adafruit_support_mike »

It sounds like you're running out of RAM.

Take a look through this tutorial for information about diagnosing and dealing with memory problems: https://learn.adafruit.com/memories-of- ... ot-dot-dot

User avatar
IAmSam
 
Posts: 5
Joined: Fri Sep 04, 2015 7:55 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by IAmSam »

Thanks for that link, I read through it and it was pretty useful.

But the above code is the only thing I am uploading to the board, and I don't see any way of making it any smaller. No matter what I do the program seems to fail as soon as AFMS.begin() is called

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by adafruit_support_mike »

Try disconnecting the motors and see if it still fails.

There are only two ways to cause a failure halfway through a Serial.print(): some kind of segmentation fault that causes the microcontroller's watchdog timer to force a reset (usually caused by RAM exhaustion), or a dip in power that causes the brownout detector to force a reset.

User avatar
IAmSam
 
Posts: 5
Joined: Fri Sep 04, 2015 7:55 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by IAmSam »

I've removed all motors from the motor shield, and I'm getting the same result - so it must be a RAM issue then.

Maybe there's an error in my code I can't spot? I don't see any way of reducing the amount of memory I'm using, it's pretty bare bones as it is

User avatar
adafruit_support_mike
 
Posts: 67446
Joined: Thu Feb 11, 2010 2:51 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by adafruit_support_mike »

Add some calls to freeRAM() that print the amount of memory on each pass through the loop. If that number keeps decreasing, it means you have a memory leak.

If the code resets without gradually losing memory, it means you probably have a data structure that's too big for the available RAM.

User avatar
IAmSam
 
Posts: 5
Joined: Fri Sep 04, 2015 7:55 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by IAmSam »

Sorry it took so long to reply, it's been a bust week.

I have attempted to use FreeRam() to check this, but the program crashes before it gets to the loop. The program only prints out the two characters Fo (from the println("Foo") in the Robot.cpp class) then stops working. If I remove AFMS.begin() from the Robot.cpp file, then it consistently prints out 1559 bytes remaining.

User avatar
adafruit_support_bill
 
Posts: 88093
Joined: Sat Feb 07, 2009 10:11 am

Re: Need help creating a wrapper class for an Arduino Robot

Post by adafruit_support_bill »

This line:

Code: Select all

Robot myBot;
implicitly calls the default (no parameters) constructor.
This line:

Code: Select all

  myBot = Robot();
calls it again explicitly. I don't think you need to do that.

User avatar
IAmSam
 
Posts: 5
Joined: Fri Sep 04, 2015 7:55 pm

Re: Need help creating a wrapper class for an Arduino Robot

Post by IAmSam »

Hi,
I changed it the code as you suggested, but still ran into the same problem. However, by creating the Adafruit_MotorShield object in the main sketch then passing it down as a parameter into the the Robot class the code runs fine, as long as I call AFMS.begin in the main class. So the code for my main sketch now looks like this:

Code: Select all

Adafruit_MotorShield AFMS;
Robot myBot = Robot(AFMS);

void setup()
{
  AFMS.begin();
}
and Robot.cpp looks like this:

Code: Select all

Robot::Robot(Adafruit_MotorShield AFMS)
{
  _AFMS = AFMS;
}
This code seems to run fine, but I am confused to as why I am unable to call AFMS.begin() in the Robot.cpp class without it crashing

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”