Quantcast
Channel: Reprap Forum - Firmware - Marlin
Viewing all articles
Browse latest Browse all 2831

Get a random seed to randomize auto file order [C++, MarlinFW] (no replies)

$
0
0
Hi,
I'm a novice programmer when it comes to C++ and I'm utterly unfamiliar with the marlin firmware when it comes to the coding part (even though I have been using and configuring Marlin for a decade).
My situation is the following: I made a kinetic sand are table, similar to the sisyphus table if you are familiar with it. I have used 3d printer components (SKR mini board, pencake steppers etc) to make a 2d mechanical rail that moves a magnet.
I have my patterns made in gcode, but these patterns are made slow, hence I'm looking for an option to randomize their order, whenever I turn on my table a different pattern will start.
My issue is since I'm unfamiliar with the firmware as a code I don't know how to get a random seed and I need help. The random(0,10); function is not truly random and has the same order every time I turn on the machine.

So here is what I have so far, I have modified line 840 in cardreader.cpp

#if DISABLED(NO_SD_AUTOSTART)
/**

    Run all the auto#.g files. Called:
        On boot after successful card init.
        From the LCD command to Run Auto Files
        /
        void CardReader::autofile_begin() {
        autofile_index = 1;
        (void)autofile_check();
        }
        /*
    Run the next auto#.g file. Called:
        On boot after successful card init
        After finishing the previous auto#.g file
        From the LCD command to begin the auto#.g files
    Return 'true' if an auto file was started
    */
    bool CardReader::autofile_check() {
    if (!autofile_index) return false;

if (!isMounted())
mount();
else if (ENABLED(SDCARD_EEPROM_EMULATION))
settings.first_load();

// Don't run auto#.g when a PLR file exists
if (isMounted() && TERN1(POWER_LOSS_RECOVERY, !recovery.valid())) {
char autoname[10];
sprintf_P(autoname, PSTR("/auto%c.g"), '0' + autofile_index - 1);
if (fileExists(autoname)) {
cdroot();
openAndPrintFile(autoname);
// Generate a random number between 2 and 10
int randomNumber = random(2,10);
autofile_index = randomNumber;
//
return true;
}
}
autofile_cancel();
return false;
}
#endif`


The above code works but random(2,10); is the same sequence at every boot so I'm at where I was originally with the sequential order.
I clearly need some random seed to randomize my numbers but I don't know how to get a random seed. For example is there a function that counts uptime in milliseconds? My homing sequence in auto0.g is always different length so this would be a good way to randomize let's say with the middle square law.
Or is there a function to read a state of an analog pin that is floating?
I hope you get my issue, I need some guidance on this and maybe if you know the answer a short code snippet as I'm just learning c++

Thank you for any input.

Viewing all articles
Browse latest Browse all 2831

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>