dimircea / RX2-TX2-RC

Control the RX2 RC module by using Arduino.
GNU General Public License v3.0
5 stars 4 forks source link

Code assistance #2

Open ashpow opened 8 years ago

ashpow commented 8 years ago

Hi there,

This is really great, excellent work!

I am very new to Arduino and hoped you could assist with the command component of your code. Your example shows a looped "forward" command, how would I loop a set of instructions? For example "Forward, right, left, forward, back, turbo" Would I just do;

/* Loop code / void loop() { // add here your commands to control the RX2 module... // for test purposes, here a continuous forward command is sent sendCommand( COMMAND_FORWARD); sendCommand( COMMAND_RIGHT); sendCommand( COMMAND_LEFT); sendCommand( COMMAND_FORWARD);

?

How do I set the duration for each motion? (how far the vehicle will travel)

Thanks, Ash

dimircea commented 8 years ago

Hi,

Well, depends on what you want to achieve. If you just loop the commands as in your example, then the commands are executed sequential and this results in a strange behavior ( in your example, the motor will turn short forward, then short right then short left then short forward, then the cycle starts again).

You can execute a command for a specified period of time by counting the time such as:

// the amount of time to execute a command (milliseconds)
long commandTime = 250;
void loop()  {
  // record start time
  long startTime  =  millis();
  do {
    // here execute command, i.e., sendCommand( COMMAND_FORWARD);
  } while ( millis() - startTime < commandTime );
}

This code can be improved and adapted to your needs, but it is a simple solution on how you can execute a command for a specified amount of time.

Related to how far the vehicle travels, this is a complicate issue because it depends on a lot of factors, such as: the type of the motor, the moving surface, the battery level, the reduction gears used in your car, etc. So the answer to this question is rather dependent on your specific car components. For example, you can try to see how many times the car wheel rotates during one second, then measure the circumference of the car wheel. Based on this, you know how much distance your car travel in one seconds (but there are other factors, such as the slipping which may occur between the car tires and the surface on which the car moves). Also this works if the car moves constantly, which in general is not the case, so you will only get an approximation of the distance traveled by your car.

Best regards, Mircea.