# Return value from x2_get_motor_current()

I was looking at the code for the X2 libraries and couldn’t find what the return value from x2_get_motor_current() relates to to. The VNH2SP30 description states that a register holds the value corresponding to 0.13V/A. So: does this return value mean that I should handle the function like this?

float current = x2_get_motor_current(0) * 0.13; /* to get current on motor 0 in amps?? */

``````// Get the running current average for the specified motor
// Note: measuring current is only possible on Orangutan X2s with VNH2SP30
// motor drivers.  The VNH3SP30 does not support current sense feedback.
unsigned char OrangutanX2::getMotorCurrent(unsigned char motor)
{
unsigned char cmd = CMD_GET_M1_CURRENT;
if (motor == MOTOR2)
cmd = CMD_GET_M2_CURRENT;
OrangutanSPIMaster::transmitAndDelay(cmd, 3);
return OrangutanSPIMaster::transmit(0);
``````

Hello.

The x2_get_motor_current() function returns an 8-bit ADC reading of the current-sense voltage, so you first need to convert that to a voltage before dividing by the 0.13 V/A conversion factor. You can convert to a current-sense voltage by multiplying by 5000mV/255=19.6. Dividing this factor by 0.13mV/mA results in an overall conversion factor of approximately 150.8mA.

Also, I recommend you avoid using floats and doubles in your programs as these add quite a bit if processing overhead and greatly increase your code size. Rather, you should be able to get reasonable resolution for your calculations by using integers or longs and working with, say, integer milliamps instead of floating-point amps:

unsigned int current_mA = x2_get_motor_current(0) * 151; // gets current in milliamps

- Ben