Go to Post A person is not defined by what mistakes they make, but more so by what they do after they realize their mistake. - Andy Baker [more]
Home
Go Back   Chief Delphi > Technical > Programming
CD-Media   CD-Spy  
portal register members calendar search Today's Posts Mark Forums Read FAQ rules

 
Closed Thread
Thread Tools Rate Thread Display Modes
  #1   Spotlight this post!  
Unread 14-02-2006, 21:45
Mike's Avatar
Mike Mike is offline
has common ground with Matt Krass
AKA: Mike Sorrenti
FRC #0237 (Sie-H2O-Bots (See-Hoe-Bots) [T.R.I.B.E.])
Team Role: Programmer
 
Join Date: Dec 2004
Rookie Year: 2004
Location: Watertown, CT
Posts: 1,003
Mike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond reputeMike has a reputation beyond repute
Motor drive function?

Lets say I want to have a function, that somehow modifies an inputted speed and sets an inputted motor to that speed. Instinct tells me it would be something similar to
Code:
void Drive(int motor, unsigned char speed)
{
	// modify inputted speed
	motor = speed;
}

Drive(pwm01, 100);
However, I've tried that and it doesn't seem to work. The motors just don't spin. I'm assuming it is because when you read pwm01, it gives you the current speed that pwm01 is running at. So Drive(pwm01, 100) is similar to, say, Drive(127, 100)

What is the cause of this problem? How can I fix it?
__________________
http://www.mikesorrenti.com/
  #2   Spotlight this post!  
Unread 14-02-2006, 21:58
Mark McLeod's Avatar
Mark McLeod Mark McLeod is offline
Just Itinerant
AKA: Hey dad...Father...MARK
FRC #0358 (Robotic Eagles)
Team Role: Engineer
 
Join Date: Mar 2003
Rookie Year: 2002
Location: Hauppauge, Long Island, NY
Posts: 8,830
Mark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond reputeMark McLeod has a reputation beyond repute
Re: Motor drive function?

You need to pass the parameter that will be changed in by address rather than value.
Code:
void Drive(int *motor, unsigned char speed)
{
	// modify inputted speed
	*motor = speed;
}
 
Drive(&pwm01, 100);
__________________
"Rationality is our distinguishing characteristic - it's what sets us apart from the beasts." - Aristotle
  #3   Spotlight this post!  
Unread 15-02-2006, 05:04
Unsung FIRST Hero
Mike Betts Mike Betts is offline
Electrical Engineer
no team
Team Role: Engineer
 
Join Date: Dec 2001
Rookie Year: 1995
Location: Homosassa, FL
Posts: 1,442
Mike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond reputeMike Betts has a reputation beyond repute
Re: Motor drive function?

Quote:
Originally Posted by Keith Watson
The original code just modifies a local copy of the argument passed in. You need to "pass by reference". Typical usage is:
Code:
void Drive(int& motor, unsigned char speed)
{
	// modify inputted speed
	motor = speed;
}
 
Drive(pwm01, 100);
Mark's example works but is less common in usage. It takes the address of the variable, passes it in as a pointer, then dereferences the pointer.
Keith,

I think that you are mixing C++ with C. in C++, "int&" is a pass by reference. In C there is no pass by reference only pass by value and a pointer is the only way to do it.

Mike



To All,

Mark's post is correct. Please read K&R chapter 5 before sliding down the perilous and provocative path of pointers.

Mike
__________________
Mike Betts

Alumnus, Team 3518, Panthrobots, 2011
Alumnus, Team 177, Bobcat Robotics, 1995 - 2010
LRI, Connecticut Regional, 2007-2010
LRI, WPI Regional, 2009 - 2010
RI, South Florida Regional, 2012 - 2013

As easy as 355/113...
  #4   Spotlight this post!  
Unread 15-02-2006, 11:55
Keith Watson Keith Watson is offline
Registered User
FRC #0957 (WATSON)
Team Role: Mentor
 
Join Date: Feb 2006
Rookie Year: 2006
Location: Wilsonville, OR
Posts: 112
Keith Watson is just really niceKeith Watson is just really niceKeith Watson is just really niceKeith Watson is just really nice
Re: Motor drive function?

Quote:
Originally Posted by Mike Betts
Keith,

I think that you are mixing C++ with C. in C++, "int&" is a pass by reference. In C there is no pass by reference only pass by value and a pointer is the only way to do it.

Mike
Oops, I stand corrected. I've been using C++ for so long (since 1988) I forgot! I removed my post.
__________________
Keith Watson - Professional Software Engineer
No relation to "Kevin" Watson, who created the camera tracking code.

Last edited by Keith Watson : 15-02-2006 at 12:20.
Closed Thread


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop time for OperatorControl function? Debug blows... Chris_Elston Programming 10 13-02-2006 14:42
Arcade Function gabrielse Programming 1 08-02-2006 00:49
TTL port to a serial port on a demo board ImmortalAres Programming 16 09-07-2005 23:44
RoboEmu2(code simulator)--now with C! rbayer Programming 23 17-02-2005 09:17
heres the code. y this not working omega Programming 16 31-03-2004 15:18


All times are GMT -5. The time now is 03:27.

The Chief Delphi Forums are sponsored by Innovation First International, Inc.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi