• 0 Votes - 0 Average
• 1
• 2
• 3
• 4
• 5
 Question on Code
01-29-2020, 05:56 AM
Post: #1
 closedsink Junior Member Posts: 12 Joined: Nov 2019 Reputation: 0
Question on Code
I've been studying the B-Robot code in order to make some changes for something I'm working on, but I see something strange that I don't understand. In the main loop part of the code, the variable "throttle" is assigned a value based on the OSCfader[0] variable, which is a "float", and equal to a value in the range 0-1.0.
The line in question is:
throttle = (OSCfader[0] - 0.5) * max_throttle;

The subtraction of 0.5 changes the range of the input to be -0.5 to +0.5. Then multiplying by max_throttle gives a larger positive or negative number.

The problem? The variable "throttle" is an *unsigned* int (uint16_t). So what is going on here? Can anyone explain how this works? Clearly it does, I just don't see how.
01-29-2020, 12:36 PM
Post: #2
 JoseJulio Moderator Posts: 29 Joined: Mar 2015 Reputation: 0
RE: Question on Code
Hi,
The throttle variable is defined as signed int16, this is the line:
int16_t throttle;
(line 159 of BROBOT_EVO2... M0.ino file. Link from assembly guide)
Maybe you are testing a different code?

Jose.

(01-29-2020 05:56 AM)closedsink Wrote:  I've been studying the B-Robot code in order to make some changes for something I'm working on, but I see something strange that I don't understand. In the main loop part of the code, the variable "throttle" is assigned a value based on the OSCfader[0] variable, which is a "float", and equal to a value in the range 0-1.0.
The line in question is:
throttle = (OSCfader[0] - 0.5) * max_throttle;

The subtraction of 0.5 changes the range of the input to be -0.5 to +0.5. Then multiplying by max_throttle gives a larger positive or negative number.

The problem? The variable "throttle" is an *unsigned* int (uint16_t). So what is going on here? Can anyone explain how this works? Clearly it does, I just don't see how.
01-29-2020, 02:29 PM
Post: #3
 JJrobots JP Administrator Posts: 686 Joined: Feb 2014 Reputation: 4
RE: Question on Code
(01-29-2020 05:56 AM)closedsink Wrote:  I've been studying the B-Robot code in order to make some changes for something I'm working on, but I see something strange that I don't understand. In the main loop part of the code, the variable "throttle" is assigned a value based on the OSCfader[0] variable, which is a "float", and equal to a value in the range 0-1.0.
The line in question is:
throttle = (OSCfader[0] - 0.5) * max_throttle;

The subtraction of 0.5 changes the range of the input to be -0.5 to +0.5. Then multiplying by max_throttle gives a larger positive or negative number.

The problem? The variable "throttle" is an *unsigned* int (uint16_t). So what is going on here? Can anyone explain how this works? Clearly it does, I just don't see how.
Are you using the code for the DEVIA board or Arduino Leonardo?
01-29-2020, 03:38 PM
Post: #4
 closedsink Junior Member Posts: 12 Joined: Nov 2019 Reputation: 0
RE: Question on Code
Oh brother,
You are right - I misread this. I was looking at this on a small screen, it was late, and it was the first time I had encountered this type of variable declaration (I had to look it up). Sorry for the confusion (mine) about this. And thanks, Jose, for straightening me out. All looks good now - moving forward!

(01-29-2020 12:36 PM)JoseJulio Wrote:  Hi,
The throttle variable is defined as signed int16, this is the line:
int16_t throttle;
(line 159 of BROBOT_EVO2... M0.ino file. Link from assembly guide)
Maybe you are testing a different code?

Jose.

(01-29-2020 05:56 AM)closedsink Wrote:  I've been studying the B-Robot code in order to make some changes for something I'm working on, but I see something strange that I don't understand. In the main loop part of the code, the variable "throttle" is assigned a value based on the OSCfader[0] variable, which is a "float", and equal to a value in the range 0-1.0.
The line in question is:
throttle = (OSCfader[0] - 0.5) * max_throttle;

The subtraction of 0.5 changes the range of the input to be -0.5 to +0.5. Then multiplying by max_throttle gives a larger positive or negative number.

The problem? The variable "throttle" is an *unsigned* int (uint16_t). So what is going on here? Can anyone explain how this works? Clearly it does, I just don't see how.
 « Next Oldest | Next Newest »

Forum Jump:

User(s) browsing this thread: 1 Guest(s)