NightRider – update e

The programming.

To capture the RC data I am using a simple Arduino function called pulseIn(). You can see all the reference on the Arduino site. I discovered this function while googling “radio control joystick to arduino” and found this great example from Sparkfun.

All the code is at github.

There is a folder called “RC_reader” with a program that captures all the 6 RC channels and prints RAW values to the console. This code it is just for debug purposes and it is not being used for anything else:

int ch1_pin = 6; // right_horizontal
int ch3_pin = 5; // right_vertical

int ch2_pin = 4; // left_vertical
int ch4_pin = 3; // left_horizontal

int ch5_pin = 7; // c_stick
int ch6_pin = 8; // right_knob

int ch1_val; 
int ch2_val;
int ch3_val; 
int ch4_val;
int ch5_val;
int ch6_val;

void setup() {
  Serial.begin(9600); 
  
  // RC pins
  pinMode(ch1_pin, INPUT);
  pinMode(ch2_pin, INPUT);
  pinMode(ch3_pin, INPUT);
  pinMode(ch4_pin, INPUT);
  pinMode(ch5_pin, INPUT);
  pinMode(ch6_pin, INPUT);

}

void loop() {

  ch1_val = pulseIn(ch1_pin, HIGH, 25000); // Read the pulse width of 
  ch2_val = pulseIn(ch2_pin, HIGH, 25000); // each channel
  ch3_val = pulseIn(ch3_pin, HIGH, 25000); // Read the pulse width of 
  ch4_val = pulseIn(ch4_pin, HIGH, 25000);
  ch5_val = pulseIn(ch5_pin, HIGH, 25000); // Read the pulse width of 
  ch6_val = pulseIn(ch6_pin, HIGH, 25000);
  
  Serial.print("ch1: ");
  Serial.print(ch1_val);
  Serial.print("  ch2: ");
  Serial.print(ch2_val);
  Serial.print("  ch3: ");
  Serial.print(ch3_val);
  Serial.print("  ch4: ");
  Serial.print(ch4_val);
  Serial.print("  ch5: ");
  Serial.print(ch5_val);
  Serial.print("  ch6: ");
  Serial.println(ch6_val);
}

Using this code, I am reading raw data from the RC, now we just need to map this data to an usable format (0 – 1023) or (0 – 255) or (0 – 180).

//The actual code have three important functions:

// map RC RAW values to usable values
rc_mapper();

// map usable values to PWM values
motionMapper();

// controls motion with IR sensor and RC control
motionControl();

rc_mapper() – maps the joystick to a differential drive, and once again google made my day, otherwise I would still be struggling with this component. This function will give two values ‘left_vertical’ and ‘right_horizontal’ that will be used later to assign speed to the motors.

motionMapper() – all the source for this function can be seen here. I had to hack this bit of code to use with the Motoruino2, because the motors are on a Slave uC, I am using a function to set PWM for both motors.

motionControl() – just uses the values that comes from the motionMapper() and sends them through the function setPWM(leftMotor, rightMotor).
I am also using the right knob on the remote controller to set maximum velocity.

There is also a Sharp Distance Sensor that is being used to avoid collisions. On the motion control I am testing the distance, and if it is below 100 the bot runs normally, if it is beyond 100 and below 200 it moves slowly, and if it is beyond 200 stops. It can always move backward, there is still some tweak I want to do here, for instance, I want to be able to enable or disable this feature in runtime.

For the light I am using one of the sticks with 3 positions. Each position gives a value, each value will correspond to a light state.

Not to much to say about the servo control. I just need to verify the course limits to avoid collisions with other components on the bot.

All the code is available and commented on GitHub.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.