Having issues with code

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Having issues with code

Post by fortyeightplus »

Hi, I'm working on a fighting robot for NXT and this is how far we've gotten with our code, it's a school project and we haven't managed to bug fix it ourselves seeing how we are both new to this programming language and we don't have much of a coding background, it's in NXC
also it's supposed to stay within a one square meter radius, or 14ft2 approximately, so i think we've made some progress but it's a bit hard.

Code: Select all

task check_sensors()
{
 while (true)
 {
   if (SENSOR_1 == 1)
   {

#define THRESHOLD 40
   task main(){
    SetSensorLowspeed(IN_1);
    SetSensorLight(IN_2);
    OnFwd(OUT_AC, 75);
    while(true){
       if (Sensor(IN_2) > THRESHOLD){
          OnRev(OUT_C, 75);
          Wait(100);
          until(Sensor(IN_2) <= THRESHOLD);
          OnFwd(OUT_AC, 75);
       }
    }
   }


#define NEAR 15 // cm
task main(){
SetSensorLowspeed(IN_1);
while(true){
OnFwd(OUT_AC, 50);
while(SensorUS(IN_1)>NEAR);
OnRev(OUT_C, 100);
Wait(800);
  }
Here's our errors:
Line 10: Error: ';' expected
Line 10: Error: Undifined Identifier main
Line 10: Error: '=' expected
Line 10: Error: Math Factor expected
Line 26: Error: ';' expected
Line 26: Error: Undifined Identifier main
Line 26: Error ''=' expected
Line 26: Error: Math Factor expected
Line 36: Error: '}' expected
Line 37: Error: '}' expected
Line 38: Error: No task named "main" exists
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Having issues with code

Post by HaWe »

you call task main inside a if-loop, that's surely a mistake ;)
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

Code: Select all

task check_sensors()
{
 while (true)
 {
 #define THRESHOLD 30
 task main(){
 if (SENSOR_1 == 1)
   {
    SetSensorLight(IN_2);
    OnFwd(OUT_AC, 75);
    while(true){
       if (Sensor(IN_2) > THRESHOLD){
          OnRev(OUT_C, 75);
          Wait(100);
          until(Sensor(IN_2) <= THRESHOLD);
          OnFwd(OUT_AC, 75);
       }
       }
    }
   }

#define NEAR 15 // cm
task main(){
SetSensorLowspeed(IN_1);
while(true){
if (SENSOR_1 == 1)
   {
OnFwd(OUT_AC, 50);
while(SensorUS(IN_1)>NEAR);
OnRev(OUT_C, 100);
Wait(800);
  }
}
}
How does this look? We still get some error messages... :?
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Having issues with code

Post by HaWe »

doc-helmut wrote:you call task main inside a if-loop, that's surely a mistake ;)
it's still quite the same
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

doc-helmut wrote:
doc-helmut wrote:you call task main inside a if-loop, that's surely a mistake ;)
it's still quite the same
Ok well what could we do to fix this? Because we are three people working on this code and we can't find a solution so it'd be really cool if you could point out what exactly is wrong with it. ;)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Having issues with code

Post by HaWe »

the general pattern is the following:

Code: Select all

task DoThis() {
  //
  //
}

task DoThat() {
  //
  //
}

task main() {
  //
  start DoThis;
  start DoThat;

  while( run_condition ) {...;}
  //
}
notice that you need to have downloaded (resp. uploaded) John Hansen's enhanced firmware to the brick instead of the original Lego firmware!

HTH! :)
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

doc-helmut wrote:the general pattern is the following:

Code: Select all

task DoThis() {
  //
  //
}

task DoThat() {
  //
  //
}

task main() {
  //
  start DoThis;
  start DoThat;

  while( run_condition ) {...;}
  //
}
notice that you need to have downloaded (resp. uploaded) John Hansen's enhanced firmware to the brick instead of the original Lego firmware!

HTH! :)
Thanks! :) What if I want to write a code for three parallell tasks then, is it the same pattern or how does it work? And why do I need to have dowloaded John Hansen's firmware? Thanks in advance :)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Having issues with code

Post by HaWe »

just add the task code for every additional task and start it in main() or where-ever.

"start" is only defined in the enhanced FW (EFW) like many other features, too.
So NXC should always only be used with the EFW on the brick. If you haven't already done it: read the BCC manual or the "Quick Guide" about the settings closely! 8-)
fortyeightplus
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Post by fortyeightplus »

doc-helmut wrote:just add the task code for every additional task and start it in main() or where-ever.

"start" is only defined in the enhanced FW (EFW) like many other features, too.
So NXC should always only be used with the EFW on the brick. If you haven't already done it: read the BCC manual or the "Quick Guide" about the settings closely! 8-)
Okay, I did have the John Hansen EFW and I've read the BCC manual and started to work on a new code with the help of your "general pattern". But I get some errors. Could you point out what is wrong and why? (Btw, I didn't know what to name the tasks 2 and 3, hope you could help) :)

Code: Select all

#define THRESHOLD 29
  task (){
     SetSensorLight(IN_2);
     OnFwd(OUT_CA,75);
     while(true){
        if (Sensor(IN_2) > THRESHOLD){
          OnRev(OUT_C, 75);
          Wait(100);
          until(Sensor(IN_2) <= THRESHOLD);
          OnFwd(OUT_AC, 75);
}

#define THRESHOLD 29
  task () {
   SetSensorLight(IN_3);
     OnFwd(OUT_AC,75);
     while(true){
        if (Sensor(IN_2) > THRESHOLD){
          OnRev(OUT_C, 75);
          Wait(100);
          until(Sensor(IN_3) <= THRESHOLD);
          OnFwd(OUT_AC, 75);
   }

task main (){

  start check_sensor;
  start 
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: Having issues with code

Post by HaWe »

you have to give names to the tasks!
just like for functions or procedures!

As I observed you also are using the sensor initialization in a wrong way, you only need to call them once in the main task, not in the secondary tasks.
maybe also try to put as much into 1 task as possible and use as few tasks as possible.

Multi-Tasking IMO is only adequate and reasonable for rather advanced programmers who are already quite experienced in using the basic stuff, not at all for beginners in any way...!

(Unfortunately I'm busy with trying to get BCC/C running on my EV3, and for this I have probably similar skills like you for NXC. So I don't work with the NXT brick and NXC any longer, and anyway - : you surely will have to test your code by yourself - but maybe some other NXC users can provide better help fo you for the moment than I am able to... )
Last edited by HaWe on 08 Nov 2013, 09:46, edited 1 time in total.
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 1 guest