Society of Robots - Robot Forum
Software => Software => Topic started by: benjammin105 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?
-
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
-
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;
}
-
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.
void grip(void)
{
int example = 1;
example = 2;
}
TICK_COUNT appControl(bla bla)
{
grip();
callSomeOtherFunc();
}
-
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.
#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;
}
-
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
-
Try moving the subroutine out of the appcontrol?
-
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.
-
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?
-
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??
-
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:
#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
-
As others have said you need to move your 'grip' function definition outside of the other definitions, ie
#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;
}