I am trying to use a micro maestro as an analog to digital converter. The following code works well if you try to set the target for a specific channel, e.g. to control a servo:
But as mentioned above I would like to read the current position of an output channel (Value between 0.00 to 255.00). Does anyone know how to get it work?
Many thanks in advance.
Hello, Andreas. First of all, you will need to use the Maestro Control Center (or UscCmd) to configure a Maestro channel as an input. This only needs to be done once.
The hard part is implementing the control transfer. You will need to look at the Usc class in UscCmd and implement your own version of the getVariables method. This will give you an array of ServoStatus structs; the element that corresponds to your channel will have the voltage reading stored in it, in the “position” field. All the structs involved are defined in Usc_protocol.cs in the Pololu USB SDK. You will probably need to read the documentation of libusb 1.0 too.
If you were using the Maestro’s virtual COM port, this would be a little more straightforward because you could just use the Get Position serial command. However, using the native USB interface is a fine choice and you should be able to get it working.
But can it be that the struct defined in Protocol.h (uscVariables) is only vor the mini maestro devices? Here is the code from the Protocol.h file:
truct servoSetting
{
int position;
int target;
unsigned int speed;
unsigned int acceleration;
};
/* uscVariables: This struct stores all the variables that can be read via
REQUEST_GET_VARIABLES.
There are 12 bytes used per servo setting
*/
struct uscVariables
{
// Fix bytecode_asm.asm if you change the order or size of
// variables in this struct.
// offset: 0
unsigned int stackPointer;
// offset: 1
unsigned int callStackPointer;
// offset: 2
unsigned int errors;
// offset: 4
unsigned int programCounter;
// offset: 6
int buffer[3]; // protects other RAM from being corrupted by improper instructions
// offset: 12
int stack[32];
// offset: 76
unsigned int callStack[10];
// offset: 96
unsigned int scriptDone; // 1 = done; 2 = about to run a single step then be done - placed here to protect against accidental overwriting of Setting
// offset: 97
unsigned int buffer2; // protects other RAM from being corrupted by improper instructions
// offset: 98
struct servoSetting servoSetting[6];
}; // total length 139 bytes
I have tried the following code to read the device variables from a micro maestro:
libusb_device_handle *device_handle;
libusb_open(this->smallDevice, &device_handle);
unsigned char* data = new unsigned char[sizeof(uscVariables)];
int length = libusb_control_transfer(device_handle, 0xC0, REQUEST_GET_VARIABLES, 0, 0, data, sizeof(uscVariables),
(ushort) 5000);
printf("Length: %d\n", length);
uscVariables variables = *(uscVariables*) data;
for (int i = 0; i < sizeof(uscVariables); i++) {
printf("%d-", *(data + i));
}
printf("\n");
for (int i = 0; i < 6; i++) {
printf("%d-", variables.servoSetting[i].target);
}
printf("\n");
libusb_close(device_handle);
Is there a Protocol.h file including a struct for the micro maestro device? Because the Usc_Protocol.cs file brings definitions for the mini and micro device.
Hello, Andreas. The first block of code you posted is not from the Maestro’s protocol.h file in the Pololu USB SDK. It is similar, but it looks like you have changed all the types from things like “u8” and “s16” to standard C types. You made several errors during that conversion, so I recommend that you double-check all the types in those two structs. In case you didn’t know, “u” stands for unsigned, “s” stands for signed, and the number after it is the number of bits. It would probably be best if you changed “u8” to “uint8_t” and “s16” to “int16_t”, etc. If I recall correctly, the protocol.h file we distribute only applies to the Micro Maestro.
Solved the problem. The data types in the Protocol.h file are not correct in regard of their size in bytes. I have attached my solution. The datatypes from the Usc_settings.cs work pretty well.
libusb_device_handle *device_handle;
libusb_open(this->smallDevice, &device_handle);
unsigned char* data = new unsigned char[sizeof(microMaestroVariables)];
int length = libusb_control_transfer(device_handle, 0xC0, REQUEST_GET_VARIABLES, 0, 0, data, sizeof(microMaestroVariables),
(ushort) 5000);
printf("Length: %d\n", length);
microMaestroVariables variables = *(microMaestroVariables*) data;
for (unsigned int i = 0; i < sizeof(microMaestroVariables); i++) {
printf("%d-", *(data + i));
}
printf("\n");
for (int i = 0; i < 6; i++) {
printf("%d-", variables.servoStatus[i].target);
}
printf("\n");
libusb_close(device_handle);