Adding BrickPi3.cpp to the header file



Is it possible to include BrickPi3.cpp in my header file?
Is there a method called “get_motor_power” to get the motor_power at the specific time and to compare it?



Hi @sumanth.gokapai,

In your header file, you can just include the header file for the BrickPi3 - BrickPi3.h. There you go: you now can use the BrickPi3 API within yours.

Thank you!


Thanks @RobertLucian for your reply.
run.cpp (44 Bytes)
test.h (96 Bytes)
test.cpp (857 Bytes)
Please look into these files in which I have included BrickPi3.h in the header file and its giving me bunch of errors when I tried to compile it and Its nothing but a simple program to get info.


You can use get_motor_status to read the current motor power.


Thanks @Matt for the reply.
When I use get_motor_status it is giving me different outputs.
If I use cout to print power it is returing me a ? symobl
If I use printf and %d, it is returning 2123489770
If I use printf and %f, it is returning a ridiculously big number!!
I know its printing the address rather than the value, but I have to get the value.
May be I was missing something, in order to compare it to a integer value, how I have to write the code??


I just modified the motors.c example program to demonstrate get_motor_status. The change isn’t merged yet, but you can see the updated example in my repo here. The example program shows how to declare the variables, how to read the values with get_motor_status, and how to display the values.


Hi @Matt I got how to declare the variables and how to read the values but I want the power value to compare it to the integer values.
Something like if(PowerA==0){ dosomething();} etc., even in the example I’m not seeing PowerA using anywhere expect for passing as an argument.

Please look into this!!


In line 56 of the example BP.get_motor_status(PORT_A, StateA, PowerA, PositionA, DPSA); the first parameter is the port getting passed to the function, and the other four parameters (StateA, PowerA, PositionA, DPSA) are variables that will be modified by the function. get_motor_status modifies the variables, so that when the function returns, the values are updated with the new values.

  uint8_t StateA;
  int8_t PowerA;
  int32_t PositionA;
  int16_t DPSA;
  // The variables are all declared, but not yet set

  // Set the variables
  BP.get_motor_status(PORT_A, StateA, PowerA, PositionA, DPSA);

  // The variables are now all set with the current status values

  // Display the values
  printf("State: %d  Power: %4d  Encoder: %6d  DPS: %6d\n", StateA, PowerA, PositionA, DPSA);

  // Compute with the values
  if(PowerA == 0){
    // Do something if motor A power is 0


If you look at declaration of get_motor_status on BrickPi3.h line 375, you’ll notice parameter 1 passes the port to the function, but parameters 2-5 pass the addresses of the variables. The function is able to push the new values to the variables at the addresses passed through parameters 2-5. Basically it’s a way of returning several values from a function.


Thanks much @matt I’ll try this and hopefully it;ll workout .


In addition to modifying the values of the variables passed, get_motor_status does also return an error code, indicating success/failure.

If you care to check and deal with the error code returned, you can do something like this:

int error_code = BP.get_motor_status(PORT_A, StateA, PowerA, PositionA, DPSA);

if(error_code != ERROR_NONE){
  // deal with the error


Thanks @Matt!!
But I got another doubt,

  1. I thought the dps means degrees per second and want to use it in my program such a way that whenever the motor stops (maybe with an obstacle), the DPS should be zero and I want to take that value to call a method/ function.
    Something like this:
    BP.get_motor_status(PORT_A,status, power, position,dps);

But instead of rotating till an obstacle comes it is rotating only till whatever the seconds specified in usleep and if I haven’t included usleep it is not even rotating. How to solve this problem.

  1. If you have used the EV3 Lego software there we can compare the motor rotation to the current and stop the motor when there is an obstacle.

I can’t figure out how it is done over here. Please help and also if I have given the motor_power value as 20, the DPS value should be constant throughout the period whenever the motor is moving but when I tried to print the values, they are varying and different from one another.

  1. Please explain what does the DPS mean and also power because I thought the power ranges from -100 to 100 but I tried to print the power value sometimes it is showing -128 which is a bit confusing!!

Sorry for the long paragraphs and I would appreciate your time and effort if you can help get through this.


  1. It looks like your code isn’t formatted correctly. While it would probably compile, I don’t think it would do what you want. If you want your code to run the motor and then wait until the motor physically stops due to an obstacle, I think your code arrangement needs to be adjusted a bit. Try something like this:
BP.set_motor_power(PORT_A, 20); // start the motor running
usleep(250000); // give the motor 250ms to start spinning
dps = 1; // not 0
while(dps != 0){
  usleep(10000); // wait 10 ms before checking again
  BP.get_motor_status(PORT_A, status, power, position, dps);

// the motor is not turning at this point

BP.set_motor_power(PORT_A, 0); // stop the motor

BP.reset_all(); // you can reset_all, but why?
  1. I’m not really sure what you mean here. Regarding the motor_power and DPS issue, set_motor_power sets the PWM output power to the motor (linear percentage, from 0% though ± 100%). This is unregulated, as the BrickPi3 will apply a constant 20% (for example) regardless of battery voltage changes, physical load on the motor, etc. . Being unregulated in this mode, the physical speed of the motor is subject to change with electrical and physical variables.

    The DPS speed calculations being done in the BrickPi3 firmware won’t be perfectly accurate and stable, especially at low speeds. Imagine that the motor is running at 25 DPS. When the firmware is re-calculating the speed at about 100Hz (every 10ms), sometimes it will see that the encoder count has increased by 1, and other times it will see that it has increased by 0 (as if not turning). If it assumes an increase by 1 means it’s running at 100dps, and an increase of 0 means it’s not turning, the values would be very unstable (constantly jumping from 0 to 100). To prevent instability to this extent, the FW implements averaging over the last 10 readings. Note in the example above, for simplicity, I implied that the FW reads 360 encoder counts per rotation, when in reality it reads 720 counts per rotation.

  2. DPS is “Degrees Per Second”. It’s a measure of rotational speed, where 6 DPS == 1 RPM (DPS is six times the precision of RPM).

    Power is the linear PWM output, as a percentage of maximum power (slightly arbitrary, as the maximum power will depend on battery voltage etc.). You can set the power using set_motor_power to run the motor at a specific power level regardless of speed, encoder position, etc. You can use -100 through 100 to set the power percentage, or -128 to float the motor. Setting the power to 0 will apply a passive brake, making the motor hard to turn, but without actually applying any power to prevent it from turning. Setting the power to -128 will float the motor, allowing it to rotate freely, as if it wasn’t connected to the BrickPi3. In the range of -128 through 127, anything outside of -100 through 100 will float the motor (e.g. 110 will float the motor just as -128 will).

If you want speed control for a more consistent speed regardless of load, battery voltage, etc. you should consider using set_motor_dps. By setting the target speed with set_motor_dps, the firmware will regulate (automatically adjust) the PWM power to attempt to run at exactly the speed specified. You can monitor the power (again, using get_motor_status) to get an idea of the load on the motor.

Try setting the DPS target to 250 DPS, and then monitor the power and DPS as you apply and remove load on the motor (perhaps use your fingers to apply load). As the load increases, the power level should increase to compensate, and the DPS should remain very close to the target speed. If you overload the motor (it’s running at full power and unable to maintain it’s target speed), bit 0x02 of the status byte will be set to indicate that it’s failing to maintain the target. You can observe all of this by running my newly updated motors.c example program. Use motor A position to control the DPS target of motor C, and observe the values of motor C.


I can’t thank you enough for explaining everything patiently!!
Thanks much!!!


Hi @sumanth.gokapai,

I believe we can then close this thread. I’ll wait for a confirmation from you beforehand.

Thank you!


Hi @RobertLucian, Can you please look into this header file problem. I still haven’t cleared this doubt.
run.cpp (44 Bytes)
Errors.txt (11.1 KB)
test.h (98 Bytes)
test.cpp (858 Bytes)

I have included all the files that I’m compiling and the errors (Error.txt).
You can see the commands that I have to used to compile in the Error.txt file.
The program is running fine when I include BrickPi3.cpp in the test.cpp file but It is giving me bunch of errors when I’m including BrickPi3.h file in Test.h

Please look into this.