Embedded C/C++

Moderators: Lundin
Number of threads: 199
Number of posts: 374

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
need help in organizing a function Posted by brad sue on 19 Sept 2008 at 4:06 PM
Hi,
I have a question about embedded C programming. I need to program drivers for a robot . I use the microprocessor freescale mc9s12c32.
the controller for the motor is the SN754410.
Here is the problem:
( I gave the whole problem so that you have an idea. the issue is the function in bold)

Motor I/O Driver. Write a motor driver module to drive the left and right motors. The module should provide at least the following operations (you may customize/combine/add to these as you see fit):
- motors_Initialization() – Initialize the PWM channels appropriately for both motors on the robot. After initialization, the motors should both be stopped and braked.
- Define four symbols: FORWARD, REVERSE, BRAKE, COAST, SAME to integer values of your choice.

motors_Control(left, dutyLeft, right, dutyRight) – where left and right will each have value FORWARD, REVERSE, BRAKE, COAST, or SAME; and dutyLeft and dutyRight are unsigned 8-bit numbers in the range 0-100, representing the percent PWM duty cycle for that motor. If the value SAME is used for either left or right, then that motor’s direction and duty cycle should remain unchanged (with the dutyLeft or dutyRight argument ignored).

I have some problems about organizing this function motors_Control(left, dutyLeft, right, dutyRight) .
I cant figure out how to distinguish left and right while assigning the parameters that will make the motor wok properly.
can some help me for the general structure of the function?

Thank you
Report
Re: need help in organizing a function Posted by Malcolm_McLean on 20 Sept 2008 at 9:39 AM
: motors_Control(left, dutyLeft, right, dutyRight) – where left and
: right will each have value FORWARD, REVERSE, BRAKE, COAST, or SAME;
: and dutyLeft and dutyRight are unsigned 8-bit numbers in the range
: 0-100, representing the percent PWM duty cycle for that motor. If
: the value SAME is used for either left or right, then that motor’s
: direction and duty cycle should remain unchanged (with the dutyLeft
: or dutyRight argument ignored).

:
/*
  control the motors. Return 0 on success, -1 on fail
  Params: - as you described
*/
int motors_control(int left, int dutyLeft, int right, int duty_right)
{
  int err;

  if(left != SAME)
    assert(dutyleft <= 100 && dutyleft >= -100);
  if(right != SAME)
    assert(dutyright <= 100 && dutyright >= -100);
  switch(left)
  {
    case FORWARD:
      err = moveleftforward(dutyleft);
      break;
    case REVERSE:
    case BRAKE:
    case COAST:
    case SAME:
  }
  switch(right)
  {
    case FORWARD:
      err = moverightforward(dutyright);
      break;
  }

  return err;
}


You don't give quite enough information. For instance is it necessary to store state information for these motors? However basically you just break up the function in manageable chunks, as above.
Report
Re: need help in organizing a function Posted by brad sue on 20 Sept 2008 at 9:53 PM

Thank you for the help!
No, it is mentioned to store the state of the motors.
The utility function like moveleftforward(dutyleft) are those which set the the PMW register right?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.