-6

I am currently trying to compute the percentage, but aren't possible to do it. The variable keep persisting at being manipulated, and stays the same number as it has read from the other variable. How come? what is the problem?

void compute_speed_profile(signed int motor_steps, unsigned int motor_accel, unsigned int motor_decel, unsigned int motor_speed)
{
  while(global_state == false)
  {
    //do nothing
    status_step_count.data = profile.current_step_position;
    chatter.publish( &status_step_count);
    nh.spinOnce();    
    moved_to_position = true;
    //motor_steps = received_data  // not currently added for testing purposes
    delay(1);

  }

    digitalWrite(en_pin,HIGH);
    delay(1);

    if (motor_steps < 0)
    {
      profile.dir = CCW;
      motor_steps = -motor_steps;
      digitalWrite(dir_pin,LOW);
    }
    else
    {
      profile.dir = CW;
      digitalWrite(dir_pin,HIGH);
    }

    delay(1);

    computation_of_speed_profile(motor_steps,motor_accel,motor_decel,motor_speed); // function should be called with the correct motor_steps value

    OCR1A = 10;
    // Set Timer/Counter to divide clock by 8
    TCCR1B |= ((0 << CS12) | (1 << CS11) | (0 << CS10));


    while(global_state == true)
    { int temp = 0; 
      cli();
      temp = profile.moved_steps;  // Keeping ISR disable shortest time possible
      sei();

      if(profile.dir == CCW)
      {
        profile.current_step_position -=  temp;
      }
      else
      {
        profile.current_step_position += temp;
      }

      //status_step_count.data = (float)(profile.current_step_position/23400.0)*100.0;//(profile.current_step_position/23400)*100; 
      status_step_count.data = (float)(profile.current_step_position/23400.0)*100.0;//map(profile.current_step_position,0,23400,0,100); // Percentage calculated here but returns 1000 being the number of steps instead of the step in percentages.. what is the problem here?
      chatter.publish( &status_step_count);

      if(moved_to_position == false) // Ensures that I am reading the newest variable
      {
        global_state = false;
      }

      nh.spinOnce();    
      delay(1);
    }     
}





ISR(TIMER1_COMPA_vect)
{
  // Holds next delay period.
  unsigned int new_first_step_delay;

  // Remember the last step delay used when accelrating.
  static int last_accel_delay;

  // Counting steps when moving.
  static unsigned int step_count = 0;

  // Keep track of remainder from new_step-delay calculation to incrase accurancy
  static unsigned int rest = 0;

  OCR1A = profile.first_step_delay;
  switch (profile.run_state)
  {

    case STOP:
      profile.moved_steps = step_count;
      step_count = 0;
      rest = 0;
      TCCR1B &= ~((1 << CS12) | (1 << CS11) | (1 << CS10)); // Stop the timer,  No clock source
      break;

    case ACCEL:
      digitalWrite(step_pin,!digitalRead(step_pin));
      //delay(1);
      //digitalWrite(step_pin,LOW);
      step_count++;
      profile.accel_count++;
      new_first_step_delay = profile.first_step_delay - (((2 * (long)profile.first_step_delay) + rest) / (4 * profile.accel_count + 1));
      rest = ((2 * (long)profile.first_step_delay) + rest) % (4 * profile.accel_count + 1);

      // Chech if we should start decelration.
      if (step_count >= profile.decel_start)
      {
        profile.accel_count = profile.decel_length;
        profile.run_state = DECEL;
      }

      // Chech if we hitted max speed.
      else if (new_first_step_delay <= profile.min_time_delay)
      {
        last_accel_delay = new_first_step_delay;
        new_first_step_delay = profile.min_time_delay;
        rest = 0;
        profile.run_state = RUN;
      }
      break;
    case RUN:
      digitalWrite(step_pin,!digitalRead(step_pin));

      //delay(1);
      //digitalWrite(step_pin,LOW);
      step_count++;
      new_first_step_delay = profile.min_time_delay;
      // Chech if we should start decelration.
      if (step_count >= profile.decel_start) 
      {
        profile.accel_count = profile.decel_length;
        // Start decelration with same delay as accel ended with.
        new_first_step_delay = last_accel_delay;
        profile.run_state = DECEL;
      }
      break;
    case DECEL:
      digitalWrite(step_pin,!digitalRead(step_pin));      
      step_count++;
      profile.accel_count++;
      new_first_step_delay = profile.first_step_delay - (((2 * (long)profile.first_step_delay) + rest)/(4 * profile.accel_count + 1));
      rest = ((2 * (long)profile.first_step_delay)+rest)%(4 * profile.accel_count + 1);
      // Check if we at last step
      //digitalWrite(step_pin,!digitalRead(step_pin));
      if(profile.accel_count >= 0)
      {
        digitalWrite(en_pin,!digitalRead(en_pin));
        profile.moved_steps = step_count;  
        moved_to_position=true;
        profile.run_state = STOP;
      }

      break;

  }
    profile.first_step_delay = new_first_step_delay;  

    if(moved_to_position==true)
    {
        moved_to_position = false;
    }
}

closed as unclear what you're asking by Mattia, jfpoilpret, KIIV, Nick Gammon Oct 16 '16 at 4:24

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    When asking questions like this, please show that you have made some effort to debug the problem. In your case, the parts of the code that deal with controlling the motor are totally irrelevant. Posting 160 lines of code and expecting people to read is not a good use of anyone's time. Also, please learn to format your code. A good style guide is google.github.io/styleguide/cppguide.html Finally, the problem is not related to Arduino at all; it's a C++ question, namely, you misunderstood what the '%' operator does. – JayEye Oct 7 '16 at 1:33
  • 2
    We were all newbies once, but try to show that you've made an effort at understanding the probem rather than throwing it out and not even pointing the line in which you suspect the error may be. – JayEye Oct 7 '16 at 1:34
  • Also asked on the Arduino Forum – Nick Gammon Oct 7 '16 at 6:52
  • Also related: variable not incrementing? – Nick Gammon Oct 7 '16 at 7:02
1

One percent is equal to 0.01. Because 0.01 isn't a whole number and if you use a integer, it will round the number either up or down. Also the % character in Arduino signifies modulo so don't think that 50% is actually 50 percent. The map function doesn't work well for me when I to calculate some percentage so don't try it.

Try:

float val = (profile.current_step_position/23400.0)*100.0;
status_step_count.data = val;

Not the answer you're looking for? Browse other questions tagged or ask your own question.