EV3 BCC / C software problem(s)
Re: EV3 BCC / C software problem(s)
ok, but I personally can't do that. I don't understand anything about Linux.
Re: EV3 BCC / C software problem(s)
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);
}
Re: EV3 BCC / C software problem(s)
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
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/
http://bricxcc.sourceforge.net/
Re: EV3 BCC / C software problem(s)
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.
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.
-
- Posts: 175
- Joined: 28 Dec 2011, 13:07
- Location: Gelderland, Netherlands
- Contact:
Re: EV3 BCC / C software problem(s)
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
http://studl.es Mindstorms Building Instructions
Re: EV3 BCC / C software problem(s)
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.
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.
-
- Posts: 175
- Joined: 28 Dec 2011, 13:07
- Location: Gelderland, Netherlands
- Contact:
Re: EV3 BCC / C software problem(s)
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
http://studl.es Mindstorms Building Instructions
Re: EV3 BCC / C software problem(s)
thanks again, Pepijn, not as complicated as I was actually afraid of.
so
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:
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 "?
so
Code: Select all
pthread_create (&aliashandle...taskname); // generate a handle (pointer)
pthread_join (aliashandle...) // join to timeslice by passing the handle (pointer)
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);
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
Re: EV3 BCC / C software problem(s)
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
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/
http://bricxcc.sourceforge.net/
Re: EV3 BCC / C software problem(s)
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?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.
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.
Who is online
Users browsing this forum: Semrush [Bot] and 4 guests