1) motors are running as expected (!)
2) nothing is shown on the display except the VM std menu
3) no reaction to button press, so the program cannot be stopped
I know I'm still stuck to the NXC syntax, so what would be the correct syntax like?
Code: Select all
// mt-demo.c
// 0.4
// 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 <pthread.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: "); NumOut(42,56, MotorRotationCount(OUT_A));
TextOut(0,48, "Enc.B: "); NumOut(42,48, MotorRotationCount(OUT_B));
TextOut(0,40, "Enc.C: "); NumOut(42,40, MotorRotationCount(OUT_C));
Wait(40);
}
pthread_exit(0);
}
inline unsigned long Random(unsigned long x) {
return (rand() % x);
}
inline void Motor(int outputs, int pwr) {
int buf=pwr;
pwr=abs(pwr);
SetPower(outputs, pwr);
if(buf>=0) {OnFwd(outputs);}
else {OnRev(outputs);}
}
void *MotorControl(void *threadid) {
int speed;
while(true) {
speed=Random(201) - 100; // ergibt eine Zufallszahl für die Motorleistung zwischen -100 und +100
Motor(OUT_A, speed);
speed=Random(201) - 100;
Motor(OUT_B, speed);
speed=Random(201) - 100;
Motor(OUT_C, speed);
Wait( 200+ (Random(2801)) ); // ergibt eine Zufallszahl für die Aktionsdauer von 200 - 3000 ms
}
pthread_exit(0);
}
main() {
int i;
unsigned short btn;
// initialize
OutputInit();
LcdInit();
ButtonLedInit();
ResetAllTachoCounts(OUT_ABCD);
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);
while(1) {
if (ButtonPressed(btn)) break;
Wait(100);
}
OutputClose();
OutputExit();
ButtonLedClose();
ButtonLedExit();
LcdExit();
}