EV3 BCC / C software problem(s)

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 BCC / C software problem(s)

Post by HaWe »

ok, but I personally can't do that. I don't understand anything about Linux.
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 BCC / C software problem(s)

Post by HaWe »

what do have to change here to make it run on the EV3?

Code: Select all

// Multitasking-Demo:
// 3 Motoren an die Motorausgänge A,B,C anschließen !
// die Motoren werden automatisch angesteuert,
// die Encoderwerte werden simultan angezeigt!

#include <stdio.h>
#include <unistd.h>

#include "ev3_constants.h"
#include "ev3_command.h"
#include "ev3_button.h"
#include "ev3_timer.h"
#include "ev3_lcd.h"
#include "ev3_sound.h"
#include "ev3_output.h"

#define clreol  DRAW_OPT_CLEAR_EOL

task  DisplayValues() {
  while(true) {
    TextOut(0,56, "Enc.A:", clreol); NumOut(42,56, MotorRotationCount(OUT_A));
    TextOut(0,48, "Enc.B:", clreol); NumOut(42,48, MotorRotationCount(OUT_B));
    TextOut(0,40, "Enc.C:", clreol); NumOut(42,40, MotorRotationCount(OUT_C));
    Wait(40);
  }
}


task MotorControl() {
  int speed;

  while(true) {
    speed=Random(201) - 100; // ergibt eine Zufallszahl für die Motorleistung  zwischen -100 und +100
    OnFwd(OUT_A,speed);

    speed=Random(201) - 100;
    OnFwd(OUT_B,speed);

    speed=Random(201) - 100;
    OnFwd(OUT_C,speed);

    Wait( 200+ (Random(2800)) ); // ergibt eine Zufallszahl für die Aktionsdauer von 200 - 3000 ms
  }
}


task main() {
  start DisplayValues;
  start MotorControl;

  while(true);

}
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: EV3 BCC / C software problem(s)

Post by afanofosc »

The main thing you will have to change is multi-tasking. If you are writing native arm-linux applications and you want them to be multi-task/multi-threaded then you will need to google and read up on how to create multiple threads in Linux. I won't be somehow magically making that as easy as creating tasks in NXC and starting them with a non-standard keyword like "start". I won't be making "task" a known keyword in C/C++. You can #define task int if you like but that will confuse you if you try to think of a function that returns an int as anything like a task in NXC.

At the moment, the EV3 API is more like the NQC API than it is like the NXC API when it comes to simple motor movement. You may recall that with NQC the speed of a motor was set separately from the commands that started a motor turning. That is the way the simplest forms of motor control API functions work currently for the EV3. So SetPower(OUT_A, 100); OnFwd(OUT_A); vs OnFwd(OUT_A, 100);.

I am still working on the LCD API so TextOut and NumOut are not fully implemented yet.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 BCC / C software problem(s)

Post by HaWe »

sorry, I may be wrong, but as far as I read the ANSI C 2011 standard supports multitasking, and that is just what the gnu c/c++ compiler is matching (pls CMIIW).

Preemptive multitasking like formerly provided by NXC is very very very essential, just like featuring sensors, motors, display, buttons, daisy chaining, sound.
My program design is generally based upon subsumption techniques and different independend background calculation tasks. So the lack of multitasking was already the one-and-only-reason why I never never used Renessas C for the RoboInterface controller by Fischertechnik and so never bought that thing finally, and that's why I changed from Fischertechnik to RCX/NQC since 1999/2000 and later on to NXT/RobotC/NXC since 2006/07.

- Without it unfortunately I won't be able to use C/BCC any longer at all: no multitasking - no BCC.
I then will have to look for different IDEs and APIs like, e.g., again RobotC or maybe (!) C# (I actually hate OOP).

I would greatly regret if I was forced to do that.
pepijndevos
Posts: 175
Joined: 28 Dec 2011, 13:07
Location: Gelderland, Netherlands
Contact:

Re: EV3 BCC / C software problem(s)

Post by pepijndevos »

Read a bit about pthreads, they allow you to do premptive multitasking on the EV3. You need to link your program with -lpthread though. I'm not sure how you would do that in BrixCC

Code: Select all

#include <stdio.h>
#include <pthread.h> 
main()  {
  pthread_t f2_thread, f1_thread; 
  void *f2(), *f1();
  int i1,i2;
  i1 = 1;
  i2 = 2;
  pthread_create(&f1_thread,NULL,f1,&i1);
  pthread_create(&f2_thread,NULL,f2,&i2);
  pthread_join(f1_thread,NULL);
  pthread_join(f2_thread,NULL);
}
void *f1(int *x){
  int i;
  i = *x;
  sleep(1);
  printf("f1: %d",i);
  pthread_exit(0); 
}
void *f2(int *x){
  int i;
  i = *x;
  sleep(1);
  printf("f2: %d",i);
  pthread_exit(0); 
}

-- Pepijn
http://studl.es Mindstorms Building Instructions
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 BCC / C software problem(s)

Post by HaWe »

at the first glance this looks promising, thank you a lot, although I actually don't understand the syntax yet.

Would it make much effort to re-write my NXC program which I posted above to run with C and pthread so that I can check it out ?
(Displaying values on the EV3 screen)

The next question for subsumption programs or relative or absolute motor position control would be: how to stop and re-start tasks at any time by different other tasks.

Then John would be requested to tell us if he will be able to make this indispensable feature work with BCC/C.
pepijndevos
Posts: 175
Joined: 28 Dec 2011, 13:07
Location: Gelderland, Netherlands
Contact:

Re: EV3 BCC / C software problem(s)

Post by pepijndevos »

Wild guess that this is how it's supposed to work:

Code: Select all

#include <pthread.h>
// Multitasking-Demo:
// 3 Motoren an die Motorausgänge A,B,C anschließen !
// die Motoren werden automatisch angesteuert,
// die Encoderwerte werden simultan angezeigt!

#include <stdio.h>
#include <unistd.h>

#include "ev3_constants.h"
#include "ev3_command.h"
#include "ev3_button.h"
#include "ev3_timer.h"
#include "ev3_lcd.h"
#include "ev3_sound.h"
#include "ev3_output.h"

#define clreol  DRAW_OPT_CLEAR_EOL

void *DisplayValues(void *threadid) {
  while(true) {
    TextOut(0,56, "Enc.A:", clreol); NumOut(42,56, MotorRotationCount(OUT_A));
    TextOut(0,48, "Enc.B:", clreol); NumOut(42,48, MotorRotationCount(OUT_B));
    TextOut(0,40, "Enc.C:", clreol); NumOut(42,40, MotorRotationCount(OUT_C));
    Wait(40);
  }
  pthread_exit(0);
}


void *MotorControl(void *threadid) {
  int speed;

  while(true) {
    speed=Random(201) - 100; // ergibt eine Zufallszahl für die Motorleistung  zwischen -100 und +100
    OnFwd(OUT_A,speed);

    speed=Random(201) - 100;
    OnFwd(OUT_B,speed);

    speed=Random(201) - 100;
    OnFwd(OUT_C,speed);

    Wait( 200+ (Random(2800)) ); // ergibt eine Zufallszahl für die Aktionsdauer von 200 - 3000 ms
  }
  pthread_exit(0);
}

main()  {
  pthread_t f2_thread, f1_thread;
  pthread_create(&f1_thread,NULL,MotorControl,NULL);
  pthread_create(&f2_thread,NULL,DisplayValues,NULL);
  pthread_join(f1_thread,NULL);
  pthread_join(f2_thread,NULL);
}
-- Pepijn
http://studl.es Mindstorms Building Instructions
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 BCC / C software problem(s)

Post by HaWe »

thanks again, Pepijn, not as complicated as I was actually afraid of.

so

Code: Select all

pthread_create (&aliashandle...taskname); // generate a handle (pointer)
pthread_join (aliashandle...) // join to timeslice by passing the handle (pointer) 
is supposed to be sth like start taskname

about how to stop (set to idle or kill) and re-start tasks at any time by different other tasks...
edit: I finally found it:

Code: Select all

 int pthread_kill(pthread_t thread, int sig);
http://man7.org/linux/man-pages/man3/pt ... ill.3.html

so there is left just one final question:

John, will you enable to include this <signal.h> and "compile and link with -pthread "?

Code: Select all

  #include <signal.h>
  //Compile and link with -pthread.
  
  pthread_create
  pthread_join
  pthread_kill 
afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: EV3 BCC / C software problem(s)

Post by afanofosc »

Of course, you will be able to include any header CodeSourcery Lite includes and you will be able to link to any library CodeSourcery Lite provides - and run it on the EV3 so long as the EV3 firmware includes the library or you put it in a "lib" folder on a micro SD card and tell CodeSourcery Lite's linker to set the RPATH to /media/card/lib. Linking to any library simply involves setting the LDFLAGS variable in the makefile template to include that library via -lpthread (that's dash L, lowercase, followed by the library name with no extension and without the "lib" that is a the start of the library filename).

Fortunately, the EV3 firmware includes pthread and most every other library that CodeSourcery Lite provides, with the exception of the C++ runtime library, which I have already posted about how to remedy.

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: EV3 BCC / C software problem(s)

Post by HaWe »

as the EV3 firmware includes the library or you put it in a "lib" folder on a micro SD card and tell CodeSourcery Lite's linker to set the RPATH to /media/card/lib. Linking to any library simply involves setting the LDPATH variable in the makefile template to include that library via -lpthread (that's dash L, lowercase, followed by the library name with no extension and without the "lib" that is a the start of the library filename).

Fortunately, the EV3 firmware includes pthread and most every other library that CodeSourcery Lite provides, with the exception of the C++ runtime library, which I have already posted about how to remedy.
sorry, I don't understand what you mean by that. Can you please make it easier to follow the instructions - or just simplify the instructions which have to be followed?
Most people (like me) don't use SD cards and don't know anything about RPATH LDPATH makefile template include library -lpthread followed by library name exception of the C++ runtime library
It all should have to work automatically when BCC once has been started, all help files and linker adjustments automatically included, just like in the old days of NQC and NXC. We all are no Linux or gpp programmers at all, that's why a very simply accesable IDE with automatic linking and making is needed.
Multitasking is a feature which must be provided by the IDE right from the start IMO.
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests