1

I write the java language. I use ev3 direct command make ev3 motor to run,

example:

"opOUTPUT_POWER,LC0(0),LC0(0x01),LC0(20), opOUTPUT_START,LC0(0),LC0(0x01)"

In this case, the power is 20 so I write value (byte)0x14 resulted ev3 forward.

when I want to make the ev3 backward, how can I do??

I think change power 20 to -20, but I don't know -20 hex value.

2

The LC0() macro treats the value as a signed 6-bit integer.

From bytecodes.h...

#define PRIMPAR_CONST_SIGN 0x20
#define PRIMPAR_VALUE 0x3F

PRIMPAR_VALUE is where the 6 bits come from. PRIMPAR_CONST_SIGN means that the 6th bit is the sign. This means it has a range of -32 to 31, inclusive.

We must also keep in mind that computers use the 2's complement for negative integer values, so we can't just use 0x34 (0x20 | 0x14) and expect it to be -20.

0x20 will be -32, which is the lowest number possible. So, to get -20, we would need -32 + 12 = -20. In 6-bit hex 0x20 + 0x0c = 0x2c. But, you don't actually have to use hex formatted number. We can let the compiler figure it out like this: (-20 & PRIMPAR_VALUE). This trims the number -20 down to 6 bits. In fact, this is what the LC0() macro does internally, so we can just write LC0(-20).

If you need values larger than this range (e.g. -100 to 100), then you need to use the LC1() macro, which uses 8-bit signed integers. It has a range of -128 to 127. So, this will work too: LC1(-20).

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.