OpenRoberta / openroberta-lab

The programming environment »Open Roberta Lab« by Fraunhofer IAIS enables children and adolescents to program robots. A variety of different programming blocks are provided to program motors and sensors of the robot. Open Roberta Lab uses an approach of graphical programming so that beginners can seamlessly start coding. As a cloud-based application, the platform can be used without prior installation of specific software but runs in any popular browser, independent of operating system and device.
Apache License 2.0
125 stars 121 forks source link

[NXT] Elements in string list cannot be set after initializing the array (NXC) #91

Closed stex closed 5 years ago

stex commented 6 years ago

Describe the bug

When trying to set an element of a string list to a new value, this value is never actually written into the underlying array.

This might be an NXC problem (as it defines the string type to avoid using an array of char), but orlab should not offer me to use this functionality if it cannot be compiled to working NXC code.

Using a basic char array instead of strings seems to work fine. As a string is an array of char (even though it's hidden behind string in NXC), I would suspect that the resulting program has problems handling the corresponding pointers. Example for a working program with char:

#define WHEELDIAMETER 5.6
#define TRACKWIDTH 12.0
#define MAXLINES 8
#include "NEPODefs.h" // contains NEPO declarations for the NXC NXT API resources

char letters[3];
task main() {
    char __letters[] = {'A', 'B', 'C'};
    letters = __letters;
    SetSensor(S1, SENSOR_TOUCH);
    SetSensor(S2, SENSOR_SOUND);
    SetSensor(S3, SENSOR_LIGHT);
    SetSensor(S4, SENSOR_LOWSPEED);
    letters[0] = 'D';
    // Using TextOut for a char doesn't work (but compiles), NumOut produces the correct ASCII code
    NumOut(0, (MAXLINES - 1) * MAXLINES, letters[0]);
    while (true) {
        if ( Sensor(S1) == true ) {
            break;
        }
        Wait(15);
    }
}

To Reproduce Steps to reproduce the behavior:

  1. Create a program like the following:

    image
  2. Run it in the simulator

  3. Get the correct output ("B")

  4. Send the program to the NXT

  5. Run it

  6. Get an empty output as the new value was never written to the array

The generated NXC code:

#define WHEELDIAMETER 5.6
#define TRACKWIDTH 12.0
#define MAXLINES 8 
#include "NEPODefs.h" // contains NEPO declarations for the NXC NXT API resources 

string letters[3];
task main() {
    string __letters[] = {"", "", ""};
    letters = __letters;
    SetSensor(S1, SENSOR_TOUCH);
    SetSensor(S2, SENSOR_SOUND);
    SetSensor(S3, SENSOR_LIGHT);
    SetSensor(S4, SENSOR_LOWSPEED);
    letters[0] = "B";
    TextOut(0, (MAXLINES - 1) * MAXLINES, letters[0]);
    while (true) {
        if ( Sensor(S1) == true ) {
            break;
        }
        Wait(15);
    }
}

Expected behavior

The first array element is set to "B" and displayed correctly.

bjost2s commented 5 years ago

probably related to #85