Author Topic: Subroutines in C  (Read 2157 times)

0 Members and 1 Guest are viewing this topic.

Offline benjammin105Topic starter

  • Jr. Member
  • **
  • Posts: 37
  • Helpful? 0
Subroutines in C
« on: March 29, 2010, 10:07:07 PM »
I'm having an issue trying to figure out how exactly I can declare a subroutine and give my robot several drive commands in the subroutine. I want to be able to call this subroutine in my code whenever I need to. Any ideas?

paulstreats

  • Guest
Re: Subroutines in C
« Reply #1 on: March 30, 2010, 02:54:54 AM »
declare a function or "subroutine" like this

void myfunction(void){

       //subroutine or function code in here

}

whenever you want to call it just put

myfunction();

into your code

Offline benjammin105Topic starter

  • Jr. Member
  • **
  • Posts: 37
  • Helpful? 0
Re: Subroutines in C
« Reply #2 on: March 31, 2010, 09:53:40 PM »
I'm not getting this to work for my gripper arm. I'm getting an error saying void value not ignored as it ought to be.



#include "sys/axon.h"
#include "a2d.h"
#include "servos.h"

//define gripper arm to ADC channel 13
//#define value ADC_NUMBER_TO_CHANNEL(13)
// Define two motors

SERVO m2a = MAKE_SERVO(FALSE, E5,1499,453); //left side
SERVO m1a = MAKE_SERVO(TRUE, E4,1500,503);  //right side
SERVO arm = MAKE_SERVO(FALSE, H4,1500,500); //gripper arm
SERVO mini = MAKE_SERVO(TRUE, H3, 875,490); //mini servo
// Create the list - remember to place an & at the start of each servo name
SERVO_LIST servos[] = {&m1a,&m2a,&arm,&mini};

// Create a driver for the list of servos

SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);

// Global variable that indicates the current state
//uint8_t state;
// Initialise the servos

void appInitHardware(void){
   servoPWMInit(&bank1);
   }
TICK_COUNT appInitSoftware(TICK_COUNT loopStart){
   return 0;
   }
TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart){
void grip(void){
act_setSpeed(&mini,29);
delay_ms(3000);
act_setSpeed(&mini,DRIVE_SPEED_BRAKE);
delay_ms(2000);
}

if(grip()){
act_setSpeed(&arm,20);
delay_ms(10000);
act_setSpeed(&arm,DRIVE_SPEED_BRAKE);
delay_ms(10000);}

//act_setSpeed(&m1a,DRIVE_SPEED_MAX);
//act_setSpeed(&m2a,DRIVE_SPEED_MAX);
return 0;
}
 

Offline richiereynolds

  • Full Member
  • ***
  • Posts: 112
  • Helpful? 3
Re: Subroutines in C
« Reply #3 on: April 01, 2010, 04:21:02 AM »
Your grip function seems to be declared inside the appControl function, move it out onto it's own. Plus your braces don't appear to be properly matched.

If you indent your code it'll be much easier to read and to see where you've made mistakes e.g.

Code: [Select]
void grip(void)
{
    int example = 1;
    example = 2;
}

TICK_COUNT appControl(bla bla)
{
    grip();
    callSomeOtherFunc();
}

Offline Soeren

  • Supreme Robot
  • *****
  • Posts: 4,672
  • Helpful? 227
  • Mind Reading: 0.0
Re: Subroutines in C
« Reply #4 on: April 01, 2010, 06:28:49 AM »
Hi,

Allways place your code within code tags and format it in a readable way - like below - as not that many people are gonna waste time on your problem code if you don't even care to make it comprehensible.
That might even make it possible for you too see what goes wrong sometimes.

Personally, I am making a case of not bothering with other peoples code as presented (more or less jumbled up), when it's clear they don't understand what they wanted the code to do and I find it extremely boring to reformat alien code, so consider this a one-off service and do it yourself in the future :)

I'm not getting this to work for my gripper arm. I'm getting an error saying void value not ignored as it ought to be.

Code: [Select]
#include "sys/axon.h"
#include "a2d.h"
#include "servos.h"

//define gripper arm to ADC channel 13
//#define value ADC_NUMBER_TO_CHANNEL(13)
// Define two motors

SERVO m2a = MAKE_SERVO(FALSE, E5,1499,453); //left side
SERVO m1a = MAKE_SERVO(TRUE, E4,1500,503);  //right side
SERVO arm = MAKE_SERVO(FALSE, H4,1500,500); //gripper arm
SERVO mini = MAKE_SERVO(TRUE, H3, 875,490); //mini servo
// Create the list - remember to place an & at the start of each servo name
SERVO_LIST servos[] = {&m1a,&m2a,&arm,&mini};

// Create a driver for the list of servos

SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);

// Global variable that indicates the current state
//uint8_t state;
// Initialise the servos

void appInitHardware(void)
   {
      servoPWMInit(&bank1);
   }

TICK_COUNT appInitSoftware(TICK_COUNT loopStart)
   {
      return 0;
   }

TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
   {
      void grip(void)
         {
            act_setSpeed(&mini,29);
            delay_ms(3000);
            act_setSpeed(&mini,DRIVE_SPEED_BRAKE);
            delay_ms(2000);
         }

   if(grip())
      {
         act_setSpeed(&arm,20);
         delay_ms(10000);
         act_setSpeed(&arm,DRIVE_SPEED_BRAKE);
         delay_ms(10000);
      }
      //act_setSpeed(&m1a,DRIVE_SPEED_MAX);
      //act_setSpeed(&m2a,DRIVE_SPEED_MAX);
      return 0;
   }
Regards,
Søren

A rather fast and fairly heavy robot with quite large wheels needs what? A lot of power?
Please remember...
Engineering is based on numbers - not adjectives

Offline benjammin105Topic starter

  • Jr. Member
  • **
  • Posts: 37
  • Helpful? 0
Re: Subroutines in C
« Reply #5 on: April 01, 2010, 11:58:25 AM »
Thats still not working. We are getting a void is not ignored as it ought to be error. I think it has to be the voids in the subroutine

Offline Razor Concepts

  • Supreme Robot
  • *****
  • Posts: 1,856
  • Helpful? 53
    • RazorConcepts
Re: Subroutines in C
« Reply #6 on: April 01, 2010, 12:08:20 PM »
Try moving the subroutine out of the appcontrol?

Offline richiereynolds

  • Full Member
  • ***
  • Posts: 112
  • Helpful? 3
Re: Subroutines in C
« Reply #7 on: April 01, 2010, 12:25:39 PM »
Could you post the exact and full error you're getting?

Also, move that function out of appControl as has been suggested a couple of times above, the nesting is allowed but in this case it just makes the code harder to read.

Offline chelmi

  • Supreme Robot
  • *****
  • Posts: 496
  • Helpful? 15
    • Current projects
Re: Subroutines in C
« Reply #8 on: April 01, 2010, 12:47:45 PM »
your grip() function returns "void" (=nothing) and you are trying to use it in a if statement.
What are you trying to test with this if statement?

Offline benjammin105Topic starter

  • Jr. Member
  • **
  • Posts: 37
  • Helpful? 0
Re: Subroutines in C
« Reply #9 on: April 01, 2010, 12:53:49 PM »
We're not actually trying to test anything actually. I'm just looking for a way to trigger some other lines of code if these lines are run. Is there another way in C to do this??

Offline KurtEck

  • Robot Overlord
  • ****
  • Posts: 217
  • Helpful? 12
Re: Subroutines in C
« Reply #10 on: April 01, 2010, 02:19:54 PM »
We're not actually trying to test anything actually. I'm just looking for a way to trigger some other lines of code if these lines are run. Is there another way in C to do this??
I am sort-of confused by your question...  Why the use of the function?  If you simply want to call it and then do the other stuff why not:

Code: [Select]
#include "sys/axon.h"
#include "a2d.h"
#include "servos.h"

//define gripper arm to ADC channel 13
//#define value ADC_NUMBER_TO_CHANNEL(13)
// Define two motors

SERVO m2a = MAKE_SERVO(FALSE, E5,1499,453); //left side
SERVO m1a = MAKE_SERVO(TRUE, E4,1500,503);  //right side
SERVO arm = MAKE_SERVO(FALSE, H4,1500,500); //gripper arm
SERVO mini = MAKE_SERVO(TRUE, H3, 875,490); //mini servo
// Create the list - remember to place an & at the start of each servo name
SERVO_LIST servos[] = {&m1a,&m2a,&arm,&mini};

// Create a driver for the list of servos

SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);

// Global variable that indicates the current state
//uint8_t state;
// Initialise the servos

void appInitHardware(void)
   {
      servoPWMInit(&bank1);
   }

TICK_COUNT appInitSoftware(TICK_COUNT loopStart)
   {
      return 0;
   }

TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
   {
      void grip(void)
         {
            act_setSpeed(&mini,29);
            delay_ms(3000);
            act_setSpeed(&mini,DRIVE_SPEED_BRAKE);
            delay_ms(2000);
         }

   grip();
   act_setSpeed(&arm,20);
   delay_ms(10000);
   act_setSpeed(&arm,DRIVE_SPEED_BRAKE);
   delay_ms(10000);

    //act_setSpeed(&m1a,DRIVE_SPEED_MAX);
    //act_setSpeed(&m2a,DRIVE_SPEED_MAX);
    return 0;
}
If you want the other code to be run condionally it needs to be done by either having Grip return something or having a condition which you test and if true, then call grip and then also do the other code...

Kurt

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: Subroutines in C
« Reply #11 on: April 02, 2010, 07:48:48 AM »
As others  have said you need to move your 'grip' function definition outside of the other definitions, ie

Code: [Select]
#include "sys/axon.h"
#include "a2d.h"
#include "servos.h"

//define gripper arm to ADC channel 13
//#define value ADC_NUMBER_TO_CHANNEL(13)
// Define two motors

SERVO m2a = MAKE_SERVO(FALSE, E5,1499,453); //left side
SERVO m1a = MAKE_SERVO(TRUE, E4,1500,503);  //right side
SERVO arm = MAKE_SERVO(FALSE, H4,1500,500); //gripper arm
SERVO mini = MAKE_SERVO(TRUE, H3, 875,490); //mini servo
// Create the list - remember to place an & at the start of each servo name
SERVO_LIST servos[] = {&m1a,&m2a,&arm,&mini};

// Create a driver for the list of servos

SERVO_DRIVER bank1 = MAKE_SERVO_DRIVER(servos);

// Global variable that indicates the current state
//uint8_t state;
// Initialise the servos

void appInitHardware(void)
   {
      servoPWMInit(&bank1);
   }

TICK_COUNT appInitSoftware(TICK_COUNT loopStart)
   {
      return 0;
   }

void grip(void)
{
     act_setSpeed(&mini,29);
      delay_ms(3000);
      act_setSpeed(&mini,DRIVE_SPEED_BRAKE);
      delay_ms(2000);
}


TICK_COUNT appControl(LOOP_COUNT loopCount, TICK_COUNT loopStart)
   {

   grip();
   act_setSpeed(&arm,20);
   delay_ms(10000);
   act_setSpeed(&arm,DRIVE_SPEED_BRAKE);
   delay_ms(10000);

    //act_setSpeed(&m1a,DRIVE_SPEED_MAX);
    //act_setSpeed(&m2a,DRIVE_SPEED_MAX);
    return 0;
}
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk