View unanswered posts | View active topics It is currently Wed Nov 13, 2019 11:20 pm






Reply to topic  [ 9 posts ] 
Cortex: Pass motor port to a function? 
Author Message
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Cortex: Pass motor port to a function?
Using a cortex. Is it possible to pass a motor port to a function? I don't see a data type for it, tried string and couldn't get it to work.

e.g.
//with Port4 defined in setup as Bank1Servo1:

Code:
void setservo(myport) {
  if vexRT(Ch7U) = 1
    motor[myport] = 127;
  else
    motor[myport] = 0;
}

main(){
  while true {
 setservo(Bank1Servo1); // (Port4)
 wait(1000);
}}


Thu Jul 09, 2015 11:29 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Cortex: Pass motor port to a function?
Hey there,

Try something like this:
Code:
#pragma config(Motor,  port1,           motor1,        tmotorVex393_HBridge, openLoop, driveLeft)
#pragma config(Motor,  port2,           motor2,        tmotorVex393_MC29, openLoop, driveRight)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

void moveMotor(tMotor motorPort)
{
  motor[motorPort] = 100;
  sleep(1000);
  motor[motorPort] = 0;
}

task main()
{
  moveMotor(motor1);
  moveMotor(motor2);
}


= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Fri Jul 10, 2015 2:42 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Re: Cortex: Pass motor port to a function?
mightor, thanks very much for the help.


When I compile, it's not happy with the data type? of tmotor. It gives an error:
**Error**:Variable 'tmotor' declaration must be qualified with type. Type 'short' used.

My code:
Code:
   void MultiButtonServo2(TVexJoysticks channel, tmotor motorPort){
  int sServoOn;
  if(vexRT[channel] == 1)
   sServoOn = 127;
  else
     sServoOn = -127;
  motor[motorPort] = sServoOn;
  }


Calling:
...
MultiButtonServo2(Btn7L,port4);
...


Sat Jul 11, 2015 2:28 pm
Profile
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Re: Cortex: Pass motor port to a function?
Changing tmotor to int seems to have worked. Thought I'd tried that in the first place, but if I did must have done something different. Now I have:

Code:
#pragma config(Motor,  port8,           Bank2Servo1,   tmotorServoStandard, openLoop)

void MultiButtonServo2(int motorPort){
  int sServoOn;
  if(vexRT[Btn8R] == 1)
   sServoOn = 127;
  else
     sServoOn = -127;
motor[motorPort] = sServoOn;

...
         MultiButtonServo2(Bank2Servo1);
...

And that works.

Thanks again for the help.


Sat Jul 11, 2015 2:40 pm
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Cortex: Pass motor port to a function?
Did you not see that it's tMotor with a capital M? Your program has a lowercase m. It really does make a difference.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jul 12, 2015 1:23 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Re: Cortex: Pass motor port to a function?
mightor wrote:
Did you not see that it's tMotor with a capital M? Your program has a lowercase m. It really does make a difference.

= Xander
Apologies, old guy but new to C. So I'm learning :) . Not used to the case sensitivity. I'll redo it and see how it goes. Thanks for seeing my noob error.


Sun Jul 12, 2015 11:40 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Cortex: Pass motor port to a function?
No wOrRiEs, jUst keEp thE caSe SenSITivitY iN miND :)

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jul 12, 2015 11:43 am
Profile WWW
Rookie

Joined: Mon Oct 21, 2013 12:22 pm
Posts: 20
Post Re: Cortex: Pass motor port to a function?
While we are on the subject, once I discovered declaring my motor parameter as int worked, I was using it in other places and it was working fine.

Whats the difference between declaring it as "int" or "tMotor" ? I don't see tMotor described in the help files dataype section so I'm not sure what it is exactly.


Sun Jul 12, 2015 11:52 am
Profile
Site Admin
Site Admin
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
Location: Rotterdam, The Netherlands
Post Re: Cortex: Pass motor port to a function?
Well, tMotor is what's known as an enum, which is really just a fancy way of giving values to a specific set of specific value types, in this case motors. That's why "int" also works, but it's not a nice way to program. Using symbolic values like "port1" and "port2" makes your code more readable.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]


Sun Jul 12, 2015 4:29 pm
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.