creating ints

Can I have the robot create an int? I wont to right a program that can create an int.

So we are not quite sure what you mean but

int test =5;
That is a program that creates an int.

I wold like to know if I can use a “new int”(c++) command.

I’m a bit puzzled too.
Do you mean instantiating objects? RobotC isn’t Object Oriented, so you wouldn’t be able to do that per se.

For all intents and purposes doing:


int *i = new int [2];
i[0] = 1;
i[1] = 2;

(The above code doesn’t work in RobotC, the below does. )
is the same as doing:


int i[2];
i[0] = 1;
i[1] = 2;

If you want to create an array of integers, use:


 int arrayOfInts = [1, 2, 3]; 

or if you would like to explicitly define the length of the array do:


int arrOfInts[3] = {1, 2, 3};

If you’d like to create an integer, do as @tabor473 suggested.

NO, ROBOTC doesn’t support dynamic memory allocations and is not a C++ compiler, it’s C.

You will need to use the stack calls to do pretty much everything.

What I wold like to do is, have the robot move around and find an object and create an int (or float) or more to hold the Encoder values of that object. Then when the object is picked up, then the int (or float)(s) are deleted (if that makes seance).

I know that RobotC is C not C++, I know that new int is used int C++, I don’t know abought C.

Since there’s no dynamic memory allocation, we can’t really “trash” a variable.
In the use-case you mentioned, what I’d do is:



// Set int "enc" to SensorValue of an encoder1. 
int enc = SensorValue[encoder1];

// Have the encoder build up some values
// "Recycle"/De-instantiate the variable if object is picked up
if (object == pickedUp) {
    int enc = 0;
}

// Repeat code once object is picked up

Obviously I’ve given you pseudo code that won’t work.

Now if you were to be using something like PID, you should reset your encoder values to 0 after every movement to get the most accurate reading.

In essence what you’re talking about can be done, but not in the same way. As aforementioned, RobotC doesn’t follow OOP principles, and therefore you can’t create instances of objects, because there are no instances to make objects out of.

You can create an array of integrers of a fixed size and assign each object you find to the array value.

The first object found would be location 0 in the array, and you would have to keep track of which object you are looking at as your offset. This is for 10 items, but you can do a bunch more than that. You can also make the “10” into a variable too but I wanted to show an array of integers here…

int my_object_location[10];
int object_counter= 0;

if (object == pickedUp) {
            // store the location in the variable in your array
            my_object_location[object_counter] = sensorValue[encoder1];
           // increment the counter so we store it in the next place
           object_counter= object_counter+1;
           // don't go bigger than your fixed array size or very bad things happen
           if(object_counter >= 10){
                        object_counter=0;
           }
}

Now you have my_object_location[0] holding the value of the encoder where you first found an object. my_object_location[1] would be the next one, etc.

Then you can get into data structures (or struct for short) that holds a set of variables together. Make an array of them and you are cooking now! But let’s get arrays of integers down first.

You are gonna want an array of structures. Here is some code I used for ball locations last year


typedef struct
{
	float x;
	float y;
} ballLocation;


ballLocation ballOne;
ballLocation ballTwo;
ballLocation ballThree;
ballLocation ballFour;
ballLocation ballFive;
ballLocation ballSix;
ballLocation ballSeven;
ballLocation ballEight;
ballLocation ballNine;
ballLocation ballTen;

void initBallLocations()
{
	ballOne.x = -1734;
	ballOne.y = 0;

	ballTwo.x = -1219;
	ballTwo.y = 0;

	ballThree.x = -609.6;
	ballThree.y = -609.6;

	ballFour.x = 0;
	ballFour.y = -1734.19;

	ballFive.x = 609.6;
	ballFive.y = -609.6;

	ballSix.x = 1219.2;
	ballSix.y = 0;

	ballSeven.x = 1734.19;
	ballSeven.y = 0;

	ballEight.x = 609.6;
	ballEight.y = 609.6;

	ballNine.x = -609.6;
	ballNine.y = 609.6;

	ballTen.x = 0;
	ballTen.y = 1734.19;
}

It would make more sense in your case to have an array of seen objects.

So taking tabor’s code and using arrays of structures it would look more like this. The array of structures will be referenced in the array offset and then the element name. Just remember to start counting at 0.

typedef struct
{
	float x;
	float y;
} ballLocation;

ballLocation ball[10];

void initBallLocations()
{
	ball[0].x = -1734;
	ball[0].y = 0;

	ball[1].x = -1219;
	ball[1].y = 0;

	ball[2].x = -609.6;
	ball[2].y = -609.6;

	ball[3].x = 0;
	ball[3].y = -1734.19;

	ball[4].x = 609.6;
	ball[4].y = -609.6;

	ball[5].x = 1219.2;
	ball[5].y = 0;

	ball[6].x = 1734.19;
	ball[6].y = 0;

	ball[7].x = 609.6;
	ball[7].y = 609.6;

	ball[8].x = -609.6;
	ball[8].y = 609.6;

	ball[9].x = 0;
	ball[9].y = 1734.19;
}

Just a matter of how you want to keep track of things. Separate variables can get confusing and a data structure makes it all nice together in one spot. You could make the structure bigger to have a heading and speed and other variables that help describe the situation. Or you could have them in different variables. At some point putting them together in a structure helps. It also bridges you to more object oriented programming like found in C++.

If Robot C had dynamic memory allocation this is really the way to go as you can add things into the array by growing the memory of it. But since you do not this is as good as it gets here…

I am new to c and c++ so that does not make seance to me.

Here are two references you can see how a struct is used. A struct is just a collection of variables of any old data type lumped together in one memory block.

You can organize all those things you want to describe that item together in one place versus separate variables. So it is handy housekeeping. Make an array of them and it is like a stack of these defined variable groups all together. Iterate through the counter and do the same thing over and over again by just moving the array offset.

You could for instance loop through all the array items to find the distance from each item to find the closest ball to your X Y location. Closest item in the loop is where you move to.

OK, thanks!

This link goes to a blank page,