0

When slot == SLOT_1 is true, speed_value is positive but when slot == SLOT_2 is true, speed_value becomes negative.

I printed the value of speed_value immediately after the statement speed_value = -speed_value and it shows a positive number. How did speed_value changed sign when it enters the slot == SLOT_2 argument?

case ENCODER_BOARD:
  if(port == 0)
  {
    uint8_t slot = readBuffer(7);
    int16_t speed_value = readShort(8);
    speed_value = -speed_value;

    if(slot == SLOT_1)
    {
      Encoder_1.setTarPWM(speed_value);
    }
    else if(slot == SLOT_2)
    {
      Encoder_2.setTarPWM(speed_value);
    }
    else if(slot == SLOT_3)
    {
      Encoder_3.setTarPWM(speed_value);
    }
    else if(slot == SLOT_4)
    {
      Encoder_4.setTarPWM(speed_value);
    }
  }

int16_t readShort(int16_t idx)
{
  valShort.byteVal[0] = readBuffer(idx);
  valShort.byteVal[1] = readBuffer(idx+1);
  return valShort.shortVal; 
}

uint8_t readBuffer(int16_t index)
{
  if(BluetoothSource == DATA_SERIAL)
  {
    return buffer[index];
  }
  else if(BluetoothSource == DATA_SERIAL2)
  {
    return bufferBt1[index];
  }
  else if(BluetoothSource == DATA_SERIAL3)
  {
    return bufferBt2[index];
  }
}

All the code are taken from here and I am not the owner. I myself still studying how these whole code works. I am hoping someone out there who have experience with these kind of implementations be able to help me out.

| improve this question | | | | |
  • 2
    The problem is not in the snippet you posted. And the file you linked to is 2831 lines long: do you really expect us to debug that? Please, post a Minimal, Complete, and Verifiable example showing your problem. – Edgar Bonet May 24 '17 at 8:22
  • That code can be improved. Is that code written or generated with 'Scratch' ? I can't find documentation why the speed_value should be negated or how that value gets into a bluetooth buffer. I'm afraid we can't help you with this. Do you need the speed_value to be always positive ? Then you can add that to the code. This a major problem in companies: when someone writes code that more or less works but has no documentation, then even a little change requires that the whole program has to be created all over again. – Jot May 25 '17 at 13:20
  • I think this is the documentation: learn.makeblock.com/megaPi But I still don't understand why the speed_value is negated. – Jot May 25 '17 at 13:36
  • @Jot the statement speed_value = -speed_value is not really my concern. I'm using megapi for robot arm tank. So both encoder motors needs to spin in opposite direction for the robot to move forward. I don't understand how speed_value becomes negative in the statement slot == SLOT_2. – zaraku27 May 25 '17 at 14:31
  • It doesn't. When there is enough memory, the problem is the combination of readShort(8) with SLOT_2. Another problem is lack of documentation, and it is generated code. This code checks if the speed is positive or negative and makes the speed always positive for the analogWrite: github.com/Makeblock-official/Makeblock-Libraries/blob/master/… That is the normal way to do it. – Jot May 26 '17 at 7:43
-2

I think I understand what's going on from here. That's why speed_value appears to change sign suddenly when entering the if...else... statement. But why speed_value == -speed_value is necessary is yet to be answered.

| improve this answer | | | | |
  • 1
    This is not an answer. Statements like this provide no help and explain nothing. – Mazaryk May 25 '17 at 15:36
  • Yeah i'm poor at explaining stuff but at least I understand now how the code works :) – zaraku27 May 26 '17 at 4:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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