2D Mapping


I tried to do simple 2D terrain mapping using HC-SR04 sensor with roboCore, but it occures too difficult for me.
I could use some help of you.
I’ve a turret above my mobile robot, with an angle rotation I change angle (for example by 5 degrees), then read the result of ultrasonic sensor, save it (to an array for example) and repeat until 360 degrees. But don’t know how to save it to array due tu different numbers or possibly other characters than integer.

Does anyone have any idea?

Hi Lutrin,

Array of integers (e.g. int16_t my_array[72]) can store 72 values in range <-32 768, 32767>.
You can store data in it by:
my_array[i] = my_sensor.getDistance();
where i differs from 0 to 71 depending on angle.

If you need further help, please post code that you have problem with.


Thank you for your reply. I created a square table of char and use trigonometry to calculate x and y coordinates.
It occures that I have another problem - sometimes my robot finishes it’s work (this loop) but sometimes it crashes with “Stack overflow: hMain” error and blinks with all the LEDs on RoboCore.
I cant find a reason. A delay slightly improves it, but still, stack overflow error happens once of three or four mappings.
Here’s the loop
All variables are integer type except sin and cos
for(angleTurret = 0; numbering<35 ; angleTurret = angleTurret+10)
radius = topSens.getDistance();
if(radius > 200)
radius = 200;
Serial.printf(“Too far, changed to 200 \n”);

            x = (int)((radius*(sin[numbering])))/4;
            y = (int)((radius*(cos[numbering])))/4;

            Serial.printf("Radius :    %d     Angle: %d Numbering: %d x: %d, y: %d\n", radius, angleTurret,numbering,x,y);

            if((25-x) < 0 || (25-y) < 0 || (25-x) > 51 || (25-x) > 51)
                x = -25;
                y = -25;
            map[25-x][25-y] = 'O';

            hMot1.rotRel(-21, 1000, false, INFINITE);

Thank you for further help

I would like to know few things:

  • what is the size of map[][]?
  • are sin and cos arrays? How do you define them? Why don’t you use functions?
  • why do you use different variables in declaration of ‘for’ loop?
  • why do you use statement like ‘(25-x) < 0’ instead of ‘25 < x’?
    Last two things probably does not have any impact on your problem, but would make your code more readable, thus easier to analyze possible errors.


  1. Size of map is [51][51] - in order to have my robot in the centre
  2. It is
    float cos[] = {1, 0.9848, 0.9397, 0.8660, 0.7660, 0.6428, 0.5000, 0.3420, …} I used only 10’ angle, function would be easier but I think it works in the same way
  3. Just an old addiction :slight_smile:
  4. My fault, too tired after all the problems


Looking at
if((25-x) < 0 || (25-y) < 0 || (25-x) > 51 || (25-x) > 51)
map[25-x][25-y] = ‘O’;
It seems that you allow to access map[51][51], but maximum is map[50][50], it is because numbering of arrays starts from 0.

Try to change
if((25-x) < 0 || (25-y) < 0 || (25-x) > 51 || (25-x) > 51)
if( 25 < x || 25 < y || -25 > x || -25 > y)
and tell me if the problem still occurs.


I must have missed it it’s clear that I tried to call a non existing array cell. My problem disappeared now, thanks.
What about the time and delays?
Would RoboCore have problems with bigger arrays for example [200][200]? Is delay still needed?
Also, is it possible to save of printf this map (array) to a text file and then read it without memory card or other?

Thank you for reply

I think delay is needed only when robot is waiting until sensor rotate to desired position.
Array of [200][200] should be ok.
Memory card is one of the options, If you do not want to use it, the simplest choice is to send data through serial port to your computer.