Hi,
I'm new here and also to C++. My background is Ruby, which simplifies everything possible. So in one hand, I had not such a hard start with basic concepts in C++. But for me not so basic concepts as pointers drive me crazy (Not so basic for me coming from a higher level language).
Reading for quite some time now in this forum and found a lot of helpful information.
Right now I'm hanging on a problem mit constructors.
I tried a lot of different approaches and none of them worked at the end. (Compiled but didn't gave me the right result).
I'm trying to code some logic for a robot. I try the OOP approach and tried to encapsulate a lot of logic in specific classes:
Body
|- Leg
|-Joint
I combine the cpp and h files for simplicity:
Body:
#define NUM_LEGS 2
#include <Arduino.h>
#include <Adafruit_PWMServoDriver.h>
#include "Leg.h"
#include "Head.h"
class Body {
private:
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x40, Wire);
public:
Leg legs[NUM_LEGS];
Head head;
Body();
void begin();
void nextStep();
};
Body::Body(){
legs[0] = Leg(Joint(pwm, 0), Joint(pwm, 1), Joint(pwm, 2));
legs[1] = Leg(Joint(pwm, 3), Joint(pwm, 4), Joint(pwm, 5));
}
void Body::begin(){
pwm.begin();
pwm.setOscillatorFrequency(27000000);
pwm.setPWMFreq(SERVO_FREQ);
for(int i = 0; i < NUM_LEGS; i++){
legs[i].begin();
}
}
void Body::nextStep(){
for(int i = 0; i < NUM_LEGS; i++){
legs[i].nextStep();
}
}
#include <Arduino.h>
#include "Joint.h"
class Leg {
private:
Joint _joint1;
Joint _joint2;
Joint _joint3;
public:
Leg();
Leg(Joint joint1, Joint joint2, Joint joint3);
void begin();
void nextStep();
};
Leg::Leg(){}
Leg::Leg(Joint joint1, Joint joint2, Joint joint3){
_joint1 = joint1;
_joint2 = joint2;
_joint3 = joint3;
}
void Leg::begin(){
_joint1.begin();
_joint2.begin();
_joint3.begin();
}
void Leg::nextStep(){
SerialUSB.println(_joint1._number);
}
#include <Arduino.h>
#include <Adafruit_PWMServoDriver.h>
#include <Servo.h>
class Joint {
private:
Adafruit_PWMServoDriver _pwm;
public:
int _number;
Joint();
Joint(Adafruit_PWMServoDriver pwm, int number);
void begin();
};
Joint::Joint(){}
Joint::Joint(Adafruit_PWMServoDriver pwm, int number){
_pwm = pwm;
_number = number;
}
void Joint::begin(){
}
This is the basic classes I use. I Initialize a Body in global scope, running body.begin() in setup and body.nextStep() in loop.
My console starts to output a mostly really big random number. I was expecting to see 0 and 3 in loop.
My intuition tells me, there is a problem in the construction or the "Reference". It's pretty new to me not seeing a exception and receive random numbers and still "running" programs.
So how do you construct such a example in C++ with working references?
I think it would be too long, when I post all the code I tried yet, so I try to explain them:
- Changed Leg-Constructor: Leg::Leg(Joint* joint1, ...); Joint* _joint1;
In Body: Joint *j = new Joint(pwm, 0); legs[0] = Leg(j, ....); - Changed assignment: Leg::Leg(Joint joint1, ...) : _joint1(joint1), ... {
- Changed Leg-Constructor: Leg::Leg(Joint &joint1, ...){
Nothing keeps to work. There are always random numbers in the output.
So how to create an object and assign it to the constructor of another object?
Thanks in advance.