I am working with ATMega2560, and I have defined the next parameters:
float x = 6400.0;
float y = 0.0;
long z = 0;
When I do the next operation:
z = lround(x*y);
and what is very strange, the result is -2147483648. This is causing a big problem because in the first step, where y is initialized to zero, this result affects to our machine behaviour. And what is stranger, this happens only the first time - then the result is correct. Also the function lround is used at the same time with other variables (other values of x), and their results are being correct all the time.
We have developed a function which is similar to plan_buffer_line function of Marlin for 3d printers. Concretely, the init part is practically the same:
void plan_buffer_line(const float &x, const float &y, const float &z, const float &e, float feed_rate, const uint8_t &extruder)
#endif //ENABLE_AUTO_BED_LEVELING
{
// Calculate the buffer head after we push this byte
int next_buffer_head = next_block_index(block_buffer_head);
// If the buffer is full: good! That means we are well ahead of the robot.
// Rest here until there is room in the buffer.
while(block_buffer_tail == next_buffer_head)
{
manage_heater();
manage_inactivity();
lcd_update();
}
#ifdef ENABLE_AUTO_BED_LEVELING
apply_rotation_xyz(plan_bed_level_matrix, x, y, z);
#endif // ENABLE_AUTO_BED_LEVELING
// The target position of the tool in absolute steps
// Calculate target position in absolute steps
//this should be done after the wait, because otherwise a M92 code within the gcode disrupts this calculation somehow
long target[4];
target[X_AXIS] = lround(x*axis_steps_per_unit[X_AXIS]);
target[Y_AXIS] = lround(y*axis_steps_per_unit[Y_AXIS]);
target[Z_AXIS] = lround(z*axis_steps_per_unit[Z_AXIS]);
target[E_AXIS] = lround(e*axis_steps_per_unit[E_AXIS]);
These last operations, the results are good in XYZ axis, but in E we have this problem. My theory is that the trouble is with the memory, which at the first, for some reason, this data is overwritten.
ajmoreno:
and what is very strange, the result is -2147483648.
That sounds like bullshit.
And it is not true.
Example sketch:
void setup() {
Serial.begin(9600);
float x = 6400.0;
float y = 0.0;
long z = 0;
z = lround(x*y);
Serial.println(z);
}
void loop() {
// put your main code here, to run repeatedly:
}