Encoder 64 bits value ?

Topics about the Software of Revolution Pi
Post Reply
jerome.jossent
Posts: 2
Joined: 08 Nov 2023, 11:16
Answers: 0

Encoder 64 bits value ?

Post by jerome.jossent »

Hi,
I'm completly new with revolution Pi.

Encoder produce a DINT value (32bits ?).
Is it an easy way to change it to 64 bits ?
If no, how should I do that (node-red, python) ?

Thank you very much
jerome.jossent
Posts: 2
Joined: 08 Nov 2023, 11:16
Answers: 0

Re: Encoder 64 bits value ?

Post by jerome.jossent »

I have tested this with node-red. Please, give me your feelings ?

in the Function "Counter64"

//ON START
global.set("counter", 0);
global.set("last_val", 0);

//ON MESSAGE
var val_str = msg.payload;
var val = parseInt(val_str);

var cnt = global.get("counter");
var val_last = global.get("last_val");

var diff = val - val_last;
if (diff < 0) { // 2^32
diff = diff + (2147483647 + 2147483648)
}

cnt = cnt + diff;

global.set("last_val", val);
global.set("counter", cnt);

var newMsg = { payload: cnt};
return newMsg;
Attachments
2023-11-08 12-24-06.png
2023-11-08 12-24-06.png (2.12 KiB) Viewed 6160 times
2023-11-08 17-03-41.png
2023-11-08 17-03-41.png (24.4 KiB) Viewed 6160 times
u.biakoup
KUNBUS
Posts: 182
Joined: 14 Apr 2022, 13:04
Answers: 2

Re: Encoder 64 bits value ?

Post by u.biakoup »

Hi Jerome,
Sure thing, let me walk you through it. The DINT data type in Revolution Pi represents a 32-bit signed integer. If you need to work with a 64-bit value, you can use a combination of two DINT values to represent it.

For example, let's say your 64-bit value is represented as HighDINT and LowDINT. You can combine them to form a 64-bit value in Python like this:

Code: Select all

python

# Assume HighDINT and LowDINT are two 32-bit values
HighDINT = 0x12345678
LowDINT = 0x9ABCDEF0

# Combine them to form a 64-bit value
combined_value = (HighDINT << 32) | LowDINT

# Now combined_value represents your 64-bit value
print(combined_value)
In this example, << is the bitwise left shift operator, and | is the bitwise OR operator. The << 32 shifts the bits of HighDINT 32 positions to the left, effectively creating the higher 32 bits of the 64-bit value. Then, the | combines this with the lower 32 bits from LowDINT.

You can adapt this logic to Node-RED or any other programming language you're using. The key is to understand how to manipulate the bits to create the desired 64-bit representation.
Best Regards

Ulrich Kouatang Biakoup | Technical Support
u.biakoup
KUNBUS
Posts: 182
Joined: 14 Apr 2022, 13:04
Answers: 2

Re: Encoder 64 bits value ?

Post by u.biakoup »

jerome.jossent wrote: 08 Nov 2023, 17:05 I have tested this with node-red. Please, give me your feelings ?

in the Function "Counter64"

//ON START
global.set("counter", 0);
global.set("last_val", 0);

//ON MESSAGE
var val_str = msg.payload;
var val = parseInt(val_str);

var cnt = global.get("counter");
var val_last = global.get("last_val");

var diff = val - val_last;
if (diff < 0) { // 2^32
diff = diff + (2147483647 + 2147483648)
}

cnt = cnt + diff;

global.set("last_val", val);
global.set("counter", cnt);

var newMsg = { payload: cnt};
return newMsg;
Your approach seems logical, especially the overflow handling. If it works well in your testing and meets your requirements, it should be a suitable solution for converting a 32-bit DINT to a 64-bit value in Node-RED.
Best Regards

Ulrich Kouatang Biakoup | Technical Support
Post Reply