Hey @frederic
Sorry for the late reply.
Essentially, when you configure the encoder, it is set to range that you specify and the initial value is set to 0. Currently, the enoder does not provide ātick infoā i.e how many ticks the encoder has moved either direction. It simply acts as a counter. We have plans in the future to add that support in sensei and gpio_protocol_lib,
However, we also have the option to set the value of an input sensei controller using the same method as that for setting LEDs. While you cannot set the initial value in the configuration, you can set it at the start of your python prog. Apologies if this is not shown in the documentation. We can get right to it,
Shown below is an example to set the encoder value.
Normalized encoder values
My configuration for the encoder is as shown below. I have set it up as as an analog_input
which means that sensei normalizes the range to a value between 0 and 1.0:
{
"id" : 23,
"enabled": true,
"name" : "rot_enc",
"sensor_type" : "analog_input",
"mode" : "on_value_changed",
"range" : [0, 10000],
"hardware" :
{
"hardware_type" : "encoder",
"polarity" : "active_low",
"pins" : [9, 8],
"delta_ticks" : 1
}
},
To set the initial value to 0.2, i send an osc command (through cmdline) to path /set_output to set the value of id 23 to 0.2. You can do the same through your python program as well.
oscsend localhost 23024 /set_output if 23 0.2
Non normalized encoder values
If you prefer to have represent the encoder with the whole range of values instead of a normalized range, you can set the senor type as range_input
as shown below. This will enable you to read or write the encoder values to anything between [0, 10000]
. Note that this will result in sensei outputting the encoder values in the osc path /sensors/range/rot_enc
{
"id" : 23,
"enabled": true,
"name" : "rot_enc",
"sensor_type" : "range_input",
"mode" : "on_value_changed",
"range" : [0, 10000],
"hardware" :
{
"hardware_type" : "encoder",
"polarity" : "active_low",
"pins" : [9, 8],
"delta_ticks" : 1
}
}
To set the value to say 400:
oscsend localhost 23024 /set_output if 23 400
A simple python example:
#!/usr/bin/env python3
import liblo
import time
SENSEI_TO_TEST_PORT = 23023
TEST_TO_SENSEI_PORT = 23024
TEST_BRIDGE_PATH = "/test_bridge/result"
ROT_ENC_ID = 23
# if set up as analog input
ROT_ENC_ANALOG_PATH = "/sensors/analog/rot_enc"
# if set up as range input
ROT_ENC_RANGE_PATH = "/sensors/range/rot_enc"
sensei_address = ('localhost', TEST_TO_SENSEI_PORT)
def handle_encoder(path, args):
encoder_val = args[0]
print (encoder_val)
def unhandled_msg_callback(path, args, types, src):
print('Unknown OSC message %s from %s' % (path, src.url))
# set some initial val to the rot enc
def set_rot_enc_initial_val():
osc_msg = liblo.Message('/set_output')
osc_msg.add(('i', ROT_ENC_ID))
# if set to analog input
osc_msg.add(('f', 0.2))
# if set to range input
#osc_msg.add(('f', 500))
liblo.send(sensei_address, osc_msg)
if __name__ == "__main__":
osc_server = liblo.ServerThread(SENSEI_TO_TEST_PORT)
#osc callback reg in case rot enc is setup as analog input
osc_server.add_method(ROT_ENC_ANALOG_PATH, 'f', handle_encoder)
#osc callback reg in case rot enc is setup as range input
#self._osc_server.add_method(ROT_ENC_RANGE_PATH, 'f', self._handle_encoder)
osc_server.add_method(None, None, unhandled_msg_callback)
osc_server.start()
set_rot_enc_initial_val()
while True:
time.sleep(1)