Reputation: 21
Basically, I'm building some code in which an array of objects (I've made the object myself) need to be accessible class wide, but initialized after some other steps. This is what I've made:
unsigned char stepPin;
unsigned char dirPin;
unsigned char sensorPin;
const char* ident;
//Object
StepperMotor :: StepperMotor(const unsigned char _stepPin,
const unsigned char _dirPin,
const unsigned char _sensorPin,
const char* _ident)
{
stepPin = _stepPin;
dirPin = _dirPin;
sensorPin = _sensorPin;
ident = _ident;
std::cout << "Motor " << ident << ": Step Pin - " << (int)stepPin << " | Direction Pin - " << (int)dirPin << std::endl;
}
#include "stepperMotor.h"
#include <iostream>
#include <wiringPi.h>
#include <thread>
#include <cstdlib>
#include <vector>
#include "stepperManager.h"
StepperMotor motors[] = {
{7, 15, 16, "HS"},
{0, 1, 2, "HL"},
{3, 4, 5, "HP"},
{12, 13, 6, "CS"},
{14, 10, 11, "VP"},
{21, 22, 26, "TC"},
{23, 24, 27, "TR"},
{25, 28, 29, "IN"}
};
StepperManager::StepperManager()
{
for (int i = 0; i < 8; i++){
motors[i].dumpData();
}
}
After the initial declaration, all the elements in the array become the last element. You can see this by looking at the output when it's run:
Output:
Motor HS: Step Pin - 7 | Direction Pin - 15
Motor HL: Step Pin - 0 | Direction Pin - 1
Motor HP: Step Pin - 3 | Direction Pin - 4
Motor CS: Step Pin - 12 | Direction Pin - 13
Motor VP: Step Pin - 14 | Direction Pin - 10
Motor TC: Step Pin - 21 | Direction Pin - 22
Motor TR: Step Pin - 23 | Direction Pin - 24
Motor IN: Step Pin - 25 | Direction Pin - 28
Declare the motor man
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
Motor IN: Step Pin - 25 | Direction Pin - 28
So, I don't fully understand why this is happening, I've tried making the array static, and switching to vectors instead, but none of it has helped. I'm afraid that I just don't know enough about the language to find the issue on my own.
It was pointed out that I missed the actual run code. Sorry about that guys.
This is the "main" file that implements the objects.
#include <iostream> // For cout and cerr
#include <cstdlib> // For atoi()
#include <cstring>
#include "stepperManager.h"
int main(int argc, char **argv)
{
std::cout << "Declare the motor man" << std::endl;
StepperManager motorMan;
return 0;
}
Upvotes: 1
Views: 152
Reputation: 2220
I think there should be something like this in your steppermotor.h
class StepperMotor{
//some more stuff
public:
unsigned char stepPin;
unsigned char dirPin;
unsigned char sensorPin;
const char* ident;
unsigned int stepVal;
}
This is how member variables are declared in c++. I think it might be usefull though to read some c++ tutorial, to learn the basics.
Upvotes: 0
Reputation: 254461
You've declared four global variables, which you change every time a StepperMotor
is created. Instead, you want these to be class members, so that each object has its own copy of them, independent of those in other objects:
class StepperMotor {
unsigned char stepPin;
unsigned char dirPin;
unsigned char sensorPin;
const char* ident;
// and constructors, member functions, etc.
};
Upvotes: 0
Reputation:
Here you want to learn a bit more about classes and how they work. In StepperMotor's
source file, you're defining file-scope global variables with external linkage. Every time you construct a StepperMotor
, you're overwriting those same variables, so all StepperMotors
are effectively accessing the same values (and hence the behavior you're seeing).
Since you have a C# background, it's like you're using static member variables for StepperMotors
here. You want non-static member variables. Simple example:
Foo.h:
// Foo.h
#ifndef FOO_H
#define FOO_H
class Foo
{
public:
explicit Foo(int value);
void print_value() const;
private:
int member_variable;
}
#endif
Foo.cpp:
// Foo.cpp
#include "Foo.h"
#include <iostream>
using namespace std;
Foo::Foo(int value): member_variable(value)
{
}
void Foo::print_value() const
{
cout << member_variable << endl;
}
main.cpp:
// main.cpp
#include "Foo.h"
int main()
{
Foo f1(123);
Foo f2(456);
Foo f3(789);
f1.print_value();
f2.print_value();
f3.print_value();
}
Output:
123
456
789
Also I see some thread usage in your example. I'd suggest, at this point, that this is like juggling razor blades. You want to kind of stick to the basics first, get the hang of the language, debugger, and then you can babystep your way towards parallelizing execution, micro-tuning with a profiler in hand, etc.
Upvotes: 1