Ronin Lesson Notes

Ronin

PIN ComponentRemarks
13Arduino LEDOUTPUT
4Blue LEDOUTPUT
6BuzzerOUTPUT
7Push ButtonINPUT
8RGB 
3IR LEDOUTPUT
9Servo Right
10Servo Left
11Servo Turret
12Servo
A0GP2D
(Distance Measuring Sensor)
Analog

Remember to include Ronin.h
The following routines are available in the respective Ronin_Task_x.ino

                Beep(int countx) 	Beep the buzzer with duration countx
                Ronin_Stop()		Stop the Ronin
                Ronin_Move_B()  	Move Back
                Ronin_Move_F()	    Move Forward
                Ronin_Move_PL()	    Pivot Left
                Ronin_Move_PR()	    Pivot Right

                    

In the Setup() don’t forget to call Chip_setup.
Chip_Setup initializes all pins.
For a minimal Setup(), it should look like this:

                      Chip_setup;
                      Beep(700); delay(40); Beep(700); 
                      Ronin_Stop();
                      ServoT.writeMicroseconds(ServoT_Pos);
                      ServoK.writeMicroseconds(ServoKMid);
                      PreRxData = -1;


                    

Getting Things Done

Task 1 : Keyboard Control

Controlling the robot movement using the keys from the keyboard

w – move forward
a – pivot left
s – stop
d – pivot right
x – move back

Example code:

                  RxData = Serial.read();
                  if ((RxData > 0) & (RxData != PreRxData)) {
                    PreRxData = RxData;

                    //Ronin movement
                    if (RxData == 115) Ronin_Stop(); //s stop
                    if (RxData == 119) Ronin_Move_F(); //w forward
                    if (RxData == 122) Ronin_Move_B(); //z back
                    if (RxData == 97) Ronin_Move_PL(); //a pivot left
                    if (RxData == 100) Ronin_Move_PR(); //d pivot right
                  }


                    
Don’t forget to declare RxData and PreRxData.
PreRxData makes sure that a key is not pressed repeatedly.
You might not want this style of control.

(Please note that you can use switch statement instead of if statements)
Source code available for download here

Task 2 : Turret control

Sweeping the turret using the following keys:

j – sweep the turret left
k – stop the current turret movement
l – sweep the turret right

The key is to be able to sweep the turret and still be able to receive the other commands from the user.

Example code:

                  //Turret Movement
                  ServoC++;
                  if (ServoC == 5000) {
                    ServoC = 0;
                    ServoT_Pos = ServoT_Pos + ServoT_Diff;
                    if (ServoT_Pos < 1000) ServoT_Pos = 1000;
                    else ServoT.writeMicroseconds(ServoT_Pos); 
                    if (ServoT_Pos > 2000) ServoT_Pos = 2000;
                    else ServoT.writeMicroseconds(ServoT_Pos); 
                  }
                  
The above code should be inside the
                if ((RxData > 0) & (RxData != PreRxData)) {

                }
                
Below the //Ronin movement
Source code available for download here

Task 3 : Basic Behaviour

Task 1 and Task 2 require manual control of the Ronin.
In Task 3 we will make the Ronin autonomous. The Ronin is expected to exhibit simple animal behaviour.

Simple animal behaviour rules.
Move forward if there are no obstacles.
If there is a stationary obstacle, move around it.
If the obstacle is not stationary, react to the obstacle:
 Move back when the obstacle moves towards Ronin.
 Move forward if the obstacle moves away.
 About turn if the obstacle refuses to move for a certain period of time(about 3 to 5 secs)

We can use a state diagram to help us with the coding:

Task_3A

Move forward if there are no obstacles (State 0).
When an object is encountered (State 1), check if the object is within range (20 to 25 cm).
If object moves away, continue to move forward (State 0).
If object stays put for a period of time (about 3 seconds) starts to beep.


We can easily add more states.
Source code available for download here

Task_3B

Move forward if there are no obstacles (State 0).
When an object is encountered (State 1), check if the object is within range (20 to 25 cm).
If object moves away, continue to move forward (State 1). If object is too close, back off (State 2).
In State 2, continue to back off until object is within range (go back to State 1).

If object stays put for a period of time (about 3 seconds) starts to beep.



Source code available for download here
Below is a more complete diagram.

Task_3C


Source code available for download here

Task 4 : Improving Task 3

You can think of other rules that you might want to add to make your bot realistic.

Some aspects that you might want to think about and implement are:
Fluid movement, accelerate and decelerate during movements.
Add randomness in the decision making (where appropriate) example, avoid always turning right every time the bot encounters a wall.
Actions such as stopping the bot to “look around” and decide to continue moving will definitely increase you bots’ IQ (or at least make it appear so).
Make the bot produce a sound as an audio indicator that it’s performing a certain action.