Documentation c api


#1

Hi. I’m new to the brickpi, got mine 2 days ago. I have it hooked up to my pi model b, the one with 256 mb ram. So far, i got the motors running. However, i cannot get the ligthsensor or the on/off sensor to work (the one with orange pushbutton). I successfully compile and run the LEGO c samples, but reading are random. I get the same random result if i unplug the power from the brickpi…
And also, i look for docs where the serial protocol is described. I’d like to have a closer look at it.

I also like to know a bit more about the datastructure used. What get filled in when?

Looking at brickpi.h i did a quick thin binding to ada. It is my language of choise for most things. Later on i’d like to implement that serial protocol nativly in ada, that is one readon i look for docs
Regards
Björn


#2

And general stuff about the api.
like this:

this snippet is from LEGO Motor test.c
while(1){
result = BrickPiUpdateValues();
if(!result){
printf("%dn",v);
if(f==1) {
if(++v > 300) f=0;
BrickPi.MotorSpeed[PORT_A]=200;
BrickPi.MotorSpeed[PORT_B]=200;
BrickPiUpdateValues();
}
else{
if(–v<0) f=1;
BrickPi.MotorSpeed[PORT_A]=-200;
BrickPi.MotorSpeed[PORT_B]=-200;
BrickPiUpdateValues();
}
}
usleep(10000);
}

this makes the motors go round.

But with a larger sleep - like 5 seconds, the
motors just spins say half a turn, and the stops,
until 5 secs has passed. then half a turn again.
Does this mean that the “200” value is more of a “turn 200 degress” than just “set motor speed to 200 and keep it so until I say otherwise” ?
/Björn


#3

Hey Bjorn,

Does this mean that the “200” value is more of a “turn 200 degress” than just “set motor speed to 200 and keep it so until I say otherwise” ?

THis line, BrickPi.MotorSpeed[PORT_A]=200; means you’re setting the motor on Port A to 200 power (out of a max of 255).

However, i cannot get the ligthsensor or the on/off sensor to work (the one with orange pushbutton). I successfully compile and run the LEGO c samples, but reading are random. I get the same random result if i unplug the power from the brickpi…

Can you post a picture of the sensor you’re trying to use? I think you’re describing the touch sensor in the second question. If the motors are responding to commands, a hardware error is unlikely; did you double check the sensors are on the correct ports?

Also, re unpluggin the power from the BrickPi: you should get an error response from the BrickPi if it’s not powered. This sentence is a little confusing, can you elaborate?


#4

Hi John, and thanks for the answers.
1 -

THis line, BrickPi.MotorSpeed[PORT_A]=200; means you’re setting the motor on Port A to 200 power (out of a max of 255).

Ok - I got that. But for how long?
To me, it seems like it moves when I
call BrickPiUpdateValues();
BUT, just for a short while.
The code above calls
BrickPi.MotorSpeed[PORT_A]=200;
BrickPiUpdateValues();
in a loop.

That, I guess, I s really my question.
How do I get it to run continuously?
And more interestingly, what is the trick to makes it rotate a fixed number of degrees?

2 - sorry if I was unclear.
I have 4 touchsensors and 2 light-sensors.
And I cannot get resonanble readings from any of them.

I connect the touchsensor to S1,
and run

it loops and prints the sensor input on screen, but its about 90 % 1’s and 10 % 0’s. even if I don’t touch the button. And it makes no difference if I press the button. still same distribution 90/10

For the light sensor I’ve tested

which also gives me random results.

I then tried

https://github.com/DexterInd/BrickPi_C/blob/master/Sensor_Examples/General%20-%20AnalogSensor%20test.c

But that did not light the leds up.
That I guess is bad. (seem to remember that from my rcx at the turn of the millennium. Yes, I’m that old)

The sensors I have are number two and three in
the second picture

3 - Error message from unpowered brickpi,
does it show even if DEBUG is not defined ?
looking at BrickPiRx in BrickPi.h I only see
num bytes received if debug is enabled.
That is 8 or 11 alternatively if I remember correctly
(at work now)

(im using BrickPi.H at
)

the BrickPi is powered from external 9v battery, but it is attached to the Raspberry Pi. Does not the mcus of the brickpi pick power from the raspberry?

I see a wonderful product here, but I miss some notes about how to use the api - more than the code samples.

I’m building a Tic-Tac-Toe bot.
I once did that with the rcx in Lejos, so that is fairly familiar grounds. Not using Lejos this time though.

/Björn


#5

hmm. just 1 out of 5 links is visible in above post :frowning:

so in the above text these links should be included -

"I connect the touchsensor to S1,
and run "
https://github.com/DexterInd/BrickPi_C/blob/master/Sensor_Examples/LEGO%20-%20TouchSensor%20test.c

"for the light sensor I used "
https://github.com/DexterInd/BrickPi_C/blob/master/Sensor_Examples/LEGO%20-%20ColorSensor%20test.c

and
"The sensors I have are number two and three in
the second picture"

http://drgraeme.net/DrGraeme-free-NXT-G-tutorials/Ch24/Ch24V1G/01%20MindStorms/01ChPhoto.htm

and

I’m using BrickPi.H at
https://github.com/DexterInd/BrickPi_C/blob/master/Sensor_Examples/BrickPi.h

Some forums have a ‘preview’ button.
It would be nice here too. :slight_smile:

/Björn


#6

Ok, lots of more googling and trial and errors has revealed :
1 - making a motor run continuously won’t work.
Timeout in comm makes it float after a while. Use a busy loop. - This might do well with tasking/threading.

2 - Found a page with reading back tachometers and resetting it. Have not tried yet but looks promising.

3 - I did not get that the brick could be powered from the pi, and vice versa. So a ran it with the pi connected to usb and the brickpi to 9 v.
By unplugging I meant remove the 9v battery. I now realize that did not unplug it. It was still alive due to power from the pi.

4 - I still have not been able to read back anything sensible from the sensors. I hooked up the touch button as a raw sensor ( I saw someone having trouble with mode TOUCH - https://pythonhosted.org/BrickPython/_modules/Sensor.html) and watched the output.

Not touching - I got the value 1023 +99% of the polls.
Looks good.
Pressing in the button gave the values 183 say 15% of the time, the rest was 1023.

press/release the button about 3 times a second gave me
95% 1023 and about 5% 183.

Later this wekk I’ll see if I get better luck with python.

/Björn


#7

Ok - using python did not help :frowning:
So I can start motors but not read back from any sensor.

So - I changed the pi itself.
The pi I’ve been using is a model B generation 1,
The one with 256 mb ram.
I change to a model B with 512 mb ram, and that did it.
I can now control the motors, and read back encoder values.
I can also read the touch-control
:slight_smile:
:slight_smile:

I think there was a change in some gpio ports between the models that can explain it.
I use the same SD-card in both pi:s, but it only works ok in the 512 mb model

/Björn


#8

Hey Bjorn, I’m really sorry about not responding, we have all be travelling for work later.

So – I changed the pi itself. The pi I’ve been using is a model B generation 1, The one with 256 mb ram. I change to a model B with 512 mb ram, and that did it. I can now control the motors, and read back encoder values. I can also read the touch-control

Indeed. The Rev1 has a different GPIO pinout. Sorry for not making this known earlier! How goes the process now?


#9

Well, I’ve been away and busy too.
But vacation is coming up and my intention is to build a plotter in lego during that time…

I got a strange phenomena.
I ported most thing to Ada - that is I call the C-routines from Ada and read the BrickPiStruct

I can set the speed to 100, and start a loop in
which I read the current encoder value, and compare it to the last.

from values 0 - ~ 500 it is great.
then it looks like below - it misses chuncks of readings. the motor keeps on running, but I am losing precision.

Any thoughts ?

/Björn

the code looks like


  loop
    Last_Encoder_Value := This_Encoder_Value;
    Brickpi.Motor_Speed(BrickPi.PORT_A, Speed);
    loop 
      Brickpi.Update_Values;  
      This_Encoder_Value := Brickpi.Encoder(BrickPi.PORT_A);
      exit when This_Encoder_Value /= 0;
      Put_Line(&quot;BAD *** This_Encoder_Value = &quot; &amp; This_Encoder_Value&#039;Img);
    end loop;  
    
    Put_Line(&quot;This_Encoder_Value/Last_Encoder_Value &quot; &amp; This_Encoder_Value&#039;Img &amp; &quot;/&quot; &amp; Last_Encoder_Value&#039;Img);
    
  end loop;


The output is

This_Encoder_Value/Last_Encoder_Value 580/ 578
This_Encoder_Value/Last_Encoder_Value 583/ 580
This_Encoder_Value/Last_Encoder_Value 586/ 583
This_Encoder_Value/Last_Encoder_Value 589/ 586
This_Encoder_Value/Last_Encoder_Value 592/ 589
This_Encoder_Value/Last_Encoder_Value 596/ 592
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
This_Encoder_Value/Last_Encoder_Value 666/ 596
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
This_Encoder_Value/Last_Encoder_Value 673/ 666
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
This_Encoder_Value/Last_Encoder_Value 684/ 673
This_Encoder_Value/Last_Encoder_Value 686/ 684
This_Encoder_Value/Last_Encoder_Value 690/ 686
This_Encoder_Value/Last_Encoder_Value 692/ 690
This_Encoder_Value/Last_Encoder_Value 695/ 692
This_Encoder_Value/Last_Encoder_Value 698/ 695
This_Encoder_Value/Last_Encoder_Value 707/ 698
This_Encoder_Value/Last_Encoder_Value 710/ 707
This_Encoder_Value/Last_Encoder_Value 712/ 710
This_Encoder_Value/Last_Encoder_Value 716/ 712
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
This_Encoder_Value/Last_Encoder_Value 739/ 716
This_Encoder_Value/Last_Encoder_Value 742/ 739
This_Encoder_Value/Last_Encoder_Value 745/ 742
This_Encoder_Value/Last_Encoder_Value 747/ 745
This_Encoder_Value/Last_Encoder_Value 750/ 747
This_Encoder_Value/Last_Encoder_Value 753/ 750
This_Encoder_Value/Last_Encoder_Value 756/ 753
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
This_Encoder_Value/Last_Encoder_Value 766/ 756
This_Encoder_Value/Last_Encoder_Value 770/ 766
This_Encoder_Value/Last_Encoder_Value 778/ 770
This_Encoder_Value/Last_Encoder_Value 782/ 778
This_Encoder_Value/Last_Encoder_Value 786/ 782
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
BAD *** This_Encoder_Value = 0
This_Encoder_Value/Last_Encoder_Value 796/ 786
This_Encoder_Value/Last_Encoder_Value 799/ 796
This_Encoder_Value/Last_Encoder_Value 802/ 799
This_Encoder_Value/Last_Encoder_Value 805/ 802


#10

Hmm, I see no problems when running the python sample- even with the delay commented out
(LEGO_Encoder_Test.py)

I should perhaps interface to python instead.
/Björn