1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Having issues with code

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.

Having issues with code

Postby fortyeightplus » 07 Nov 2013, 08:59

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
fortyeightplus
 
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Postby doc-helmut » 07 Nov 2013, 09:20

you call task main inside a if-loop, that's surely a mistake ;)
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2425
Joined: 29 Sep 2010, 14:25

Re: Having issues with code

Postby fortyeightplus » 07 Nov 2013, 09:33

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... :?
fortyeightplus
 
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Postby doc-helmut » 07 Nov 2013, 09:37

doc-helmut wrote:you call task main inside a if-loop, that's surely a mistake ;)

it's still quite the same
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2425
Joined: 29 Sep 2010, 14:25

Re: Having issues with code

Postby fortyeightplus » 07 Nov 2013, 09:39

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. ;)
fortyeightplus
 
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Postby doc-helmut » 07 Nov 2013, 09:46

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! :)
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2425
Joined: 29 Sep 2010, 14:25

Re: Having issues with code

Postby fortyeightplus » 07 Nov 2013, 15:09

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 :)
fortyeightplus
 
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Postby doc-helmut » 07 Nov 2013, 15:38

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-)
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2425
Joined: 29 Sep 2010, 14:25

Re: Having issues with code

Postby fortyeightplus » 07 Nov 2013, 16:31

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
fortyeightplus
 
Posts: 8
Joined: 07 Nov 2013, 08:46

Re: Having issues with code

Postby doc-helmut » 07 Nov 2013, 16:48

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 doc-helmut on 08 Nov 2013, 09:46, edited 1 time in total.
regards,
HaWe
±≠≈αγδεωΔΦΣ∫√∅∞
NXC CHESS for NXT: http://www.mindstormsforum.de/viewtopic.php?f=70&t=6790
indispensable for NXC + EV3-C: easy + trouble-free network & sensor+motor remote control for rs485, BT, USB!
User avatar
doc-helmut
 
Posts: 2425
Joined: 29 Sep 2010, 14:25

Next

Return to Mindstorms Software

Who is online

Users browsing this forum: No registered users and 2 guests