make s_value 1.11 fixed pointto use no extra memory in Block
authorJim Morris <morris@wolfman.com>
Mon, 1 Aug 2016 03:40:12 +0000 (20:40 -0700)
committerJim Morris <morris@wolfman.com>
Mon, 1 Aug 2016 03:40:12 +0000 (20:40 -0700)
src/modules/robot/Block.h
src/modules/robot/Planner.cpp
src/modules/tools/laser/Laser.cpp

index 22d5217..5888447 100644 (file)
@@ -47,9 +47,6 @@ class Block {
         uint32_t total_move_ticks;
         std::bitset<k_max_actuators> direction_bits;     // Direction for each axis in bit form, relative to the direction port's mask
 
-        // for laser TODO may be able to use 10 bits and put it in bit struct below
-        float s_value;
-
         // this is the data needed to determine when each motor needs to be issued a step
         using tickinfo_t= struct {
             int32_t steps_per_tick; // 2.30 fixed point
@@ -75,5 +72,6 @@ class Block {
             bool is_g123:1;                      // set if this is a G1, G2 or G3
             volatile bool is_ticking:1;          // set when this block is being actively ticked by the stepticker
             volatile bool locked:1;              // set to true when the critical data is being updated, stepticker will have to skip if this is set
+            uint16_t s_value:12;                 // for laser 1.11 Fixed point
         };
 };
index 4cf3cc7..1432e44 100644 (file)
@@ -78,7 +78,7 @@ bool Planner::append_block( ActuatorCoordinates &actuator_pos, uint8_t n_motors,
     }
 
     // info needed by laser
-    block->s_value = s_value;
+    block->s_value = roundf(s_value*(1<<11)); // 1.11 fixed point
     block->is_g123 = g123;
 
     // use default JD
index 1562dea..54b4cdc 100644 (file)
@@ -126,6 +126,7 @@ float Laser::current_speed_ratio(const Block *block) const
     }
 
     // figure out the ratio of its speed, from 0 to 1 based on where it is on the trapezoid
+    // FIXME steps_per_tick can change at any time, potential race condition if it changes while being read here
     float ratio= (float)block->tick_info[pm].steps_per_tick / block->tick_info[pm].plateau_rate;
 
     return ratio;
@@ -137,11 +138,13 @@ bool Laser::get_laser_power(float& power) const
     const Block *block = StepTicker::getInstance()->get_current_block();
 
     // Note to avoid a race condition where the block is being cleared we check the is_ready flag which gets cleared first,
-    // as this is an interrupt if that flag is not clear then it cannot be cleared while this is running and th eblock will still be valid (albeit it may have finished)
+    // as this is an interrupt if that flag is not clear then it cannot be cleared while this is running and thblock will still be valid (albeit it may have finished)
     if(block != nullptr && block->is_ready && block->is_g123) {
-        float requested_power = block->s_value / this->laser_maximum_s_value;
+        float requested_power = ((float)block->s_value/(1<<11)) / this->laser_maximum_s_value; // s_value is 1.11 Fixed point
+
         // Ensure we can't exceed maximum power
-        if (requested_power > 1) requested_power = 1;
+        if (requested_power < 0) requested_power = 0;
+        else if (requested_power > 1) requested_power = 1;
 
         float ratio = current_speed_ratio(block);
         power = requested_power * ratio;