For hybrid, we’re using encoders on our drive train as well as various other sensors to place ourselves on an imaginary (x,y) coordinate grid on the field.
I have created code that allows you to store a list of points. It consists of a structure with two values (x and y) as well as a pointer to point to the next structure of the same type.
Basically, coodrinates are inserted into the heap using dynamic memory allocation (using the malloc funciton). Using the functions I created, these lists are then strung together using pointers. Using pointers, you can insert another block between two blocks in the list (say if we have to navigate around something) or delete blocks.
Look at my code for details. I’ve commented it as best I can.
I have seen this type of data structure called either Link Listing or Memory Blocks.
All of my code has been taken almost line-for-line by How to Program C by H.M. Deitel and P.M. Deitel, with a few cosmetic changes.
My problem is that despite including stdio.h and stdlib.h, the compiler claims that the funcion malloc does not exist, giving me the “use of function without prototype” and then an entire slew of errors.
Upon further inspection, I found that the default stdlib.h and stdio.h don’t even contain a function prototype for malloc!
I am using MPLAB
Is there something I’m missing? Any help would be Greatly appreciated. My code is attached to this post.
EDIT: After searching a bit I found this. Can I use this in the way I want to?
I’m sorry I didn’t do some more searching before I posted. Thank you very much. However, how would it be possible to use the stack for my purposes? The point is that I don’t want to have a fixed number of my memory blocks, which unless there’s a way I’m not thinking of, is impossible in stack memory.
You could probably implement a faux “malloc”… you would just need a huge array, and some managing functions. It shouldn’t be that difficult to do, you need to avoid issues such as fragmentation if you overuse it.
It is totally possible in stack memory, as stack memory is no different from heap memory… it’s just a list of bits.
-Salik
The thing is, you do have a fixed amount of memory, because of the limitations of the hardware. This isn’t like the 4GB address space you’re used to on a PC. I think you’re far better off taking a static block of memory that is as big as you’ll ever use, and build your data structure in there. It’s not like you have to share your memory with other applications. I really can’t think of anything that you could feasibly do with dynamic memory on the IFI controller that you couldn’t also do with static memory, and it seems like you’d save yourself a huge pain in the process.
One other thing that we’ve found over the years is that pointer arithmetic on the RC processor is extremely costly. You may want to look at the .lst file that is generated when you build to see how much assembly is generated for pointer operations that you wouldn’t bat an eye at on a desktop computer.
You may be better off storing an array of points as opposed to a linked list. If your points are sequential, there’s no need to keep anything other than a simple index variable. If you’re going to jump around inside the array, you’ll need at least a second variable to index to your next point.
I would use an array or shove the coordinates in the EEPROM. It takes a bit to read, but you can queue up values as you do other things. I’d probably make my “stack” 3 objects deep. (No calculated reason, just seems right to me… )
Yes, I realize that I am using a fixed amount of memory regardless. My main concern was being able to insert points within my list, as opposed to having a set sequence of points. I suppose this could be done with a three-dimensional array. The first two dimensions could hold my coordinates, and the third dimension could determine which indice of the array comes next. If I really want, I could also use some bitwise with the upper digit of the third value to determine if that array indice is in use.