Chief Delphi

Chief Delphi (http://www.chiefdelphi.com/forums/index.php)
-   Programming (http://www.chiefdelphi.com/forums/forumdisplay.php?f=51)
-   -   Pointer to pwm (http://www.chiefdelphi.com/forums/showthread.php?t=63349)

bronxbomber92 05-02-2008 19:37

Pointer to pwm
 
I just wanted to make sure this was safe to do.

Code:

typedef struct
{
    // stuff
    unsigned char* pwm;
} Motor;

void InitMotor( Motor* motor, unsigned char* pwm, /* more parameters */ )
{
    // stuff
    motor->pwm = pwm;
}

then if I want to change the motor speed
Motor myMotor; InitMotor( myMoter, pwm10 );
myMotor.pwm = 127; // set it to nuetral

Is that all right to do? I assume it is, but just want to make sure.

Thanks

dcbrown 05-02-2008 19:56

Re: Pointer to pwm
 
Code:

typedef struct
{
    // stuff
    unsigned char* pwm;
} Motor;

void InitMotor( Motor* motor, unsigned char* pwm, /* more parameters */ )
{
    // stuff
    motor->pwm = pwm;
}

then if I want to change the motor speed
Motor myMotor; InitMotor( &myMotor, &pwm10 );
*myMotor.pwm = 127; // set it to nuetral

need * to dereference the pointer and & to pass a pointer in the places shown

bronxbomber92 05-02-2008 20:03

Re: Pointer to pwm
 
Nope, you're right!

Thanks

Jon Stratis 05-02-2008 23:36

Re: Pointer to pwm
 
that seems like an OK way to do it... what might end up being easier, though, is to just do something like:

#define leftMotor pwm01
#define rightMotor pwm02

then you can use your left and right motors wherever you want, and if someone plugs something in backwards and doesn't want to unplug it you just have to change it in the one location in the header file. Just another option to consider!

JohnC 06-02-2008 00:24

Re: Pointer to pwm
 
We use Left(int) and Right(int)
Code:

void Left(int speed) {
  pwm01 = speed;
 
  if(pwm01 > 254) {
    pwm01 = 254;
  } else if(pwm01 < 1) {
    pwm01 = 1;
  }
}

And the right one assigns 254-speed. Just yet another form of abstraction :]

bronxbomber92 07-02-2008 15:57

Re: Pointer to pwm
 
Quote:

Originally Posted by eagle33199 (Post 693011)
that seems like an OK way to do it... what might end up being easier, though, is to just do something like:

#define leftMotor pwm01
#define rightMotor pwm02

then you can use your left and right motors wherever you want, and if someone plugs something in backwards and doesn't want to unplug it you just have to change it in the one location in the header file. Just another option to consider!

That's what I have currently, and I've debating if I should use a more OO approach, which I'm still not sure of. Just brain storming :)


All times are GMT -5. The time now is 13:22.

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