Let's Make Robots! | RobotShop

Simulated 2DoF Robotic Arm playing Ping Pong using KNN Regression (Supervised Learning)

Demo at: https://www.youtube.com/watch?v=Iuulss4g46Y

Code at : https://github.com/SriramEmarose/KNN_RoboticPingPong

Robotics!: always an interesting field to work on for any Engineer. Be it a small school project robot or a huge Industrial robot, it is always exciting and fun to watch them in action. In robotics, there is an action of robot called “Emergent Behaviour”; in which the robot does surprisingly a different set of operations than it is programmed to!. This may happen because of various reasons. With exponential growth in the domain of Machine Learning, its application is seen almost everywhere nowadays right from home automation to medical diagnosis.

One such application I have worked out over the weekend is to “Teach a robot to play with a ball” (Tennis, ping pong etc). Currently, there are several methods including “Deep Neural Networks” to do this same task. To begin with, I have implemented a simple Regression method that enables the robot to predict the movement it needs to perform to hit a ball that is thrown towards it. There are several regression methods like,

Linear Least Square Fitting

Regularized Least Square Fitting

Polynomial Fitting

KNN.. etc

The one I have used is KNN (K Nearest neighbour) regression. There are several good math background of KNN like (this). We have a saying called “You are the average of 4 people around you”; this is exactly what the KNN Regression/Classification model does (Here, the number '4' is the value of 'K'). Given any prediction query, the model looks for the values that is closer to the queried value and arrives at a prediction value. In simple terms, given a table of values, find the best match of a new value that can be placed near an already known value.

With this knowledge, lets build a 2 DoF(Degree of Freedom) robotic arm that can hit a ball that is thrown at it. How do we do that?

A.Train the robot (Same as you teach a kid to place his/her hands based on the ball position)

B.Testing (See if it hits the ball correctly and re-train for more time if needed)

A. Training:

The Method I have followed is,








With this approach, the robot learns, how it needs to place it self, given a ball position.

B. Testing:

Testing is straight forwared!. Feed the ball position to the robot and let the robot hit the ball from its previous learnings.

Work Flow:














1. Design a 2 DoF Robotic Arm:

This is done using “Forward Kinematics” equation and a simple 2D plot

x2 = L1 cos(theta1) + L2 cos (theta1 + theta2)

y2 = L1 sin(theta1) + L2 sin (theta1 + theta2)


L1 and L2 are the length of the robot arm links 1 and 2 respectively















“How do we plot from the kinematics equation given above?”

Think of the robot as 2 line segments connected each other at one end. Our origin is fixed to [0,0]. So, what we need is [x1,y1] and [x2,y2]. The above forward kinematics equation gives [x2 and y2], which is what is really needed to place the robot so as to hit the ball (assuming our gripper is at [x2,y2]. Here, we also get [x1, y1] to make a plot to visualize what is happening and this can be obtained from the first block of the above equation.

X1 = L1 cos(theta1)

Y1 = L1 sin(theta1)

Once we get these parameters, using the forward kinematics equation, we can move the robot to any position given theta values

2. Function to simulate a ball within Robot's workspace:

This is simple!. We know L1 and L2 of the robot. From this we can arrive at the total distance that the robot can aim to hit the ball. So, our random position of the ball must be within this region

3. Collect data:

This is shown in section (A. Training).

4. KNN Regression:

Now comes interesting machine learning part. What data can we use to train our robot to hit the ball?. Here, I have used the set of pairs of theta and its associated ground truth of ball hit coordinates. Sample table is shown below,



Ball_X Ground Truth

Ball_Y Ground Truth





From this, the robot will learn, for a given range of Ball coordinates (x,y), what theta1 & theta2 it can take in order to hit the ball. This conclusion can be arrived with a simple mean or distance based methods


With about 20 minutes of training, the robot can achieve nearly 90% hit rate (only 11 misses in 110 balls) which is great for a hobby project :)



Lot more parameters like, force to hit the ball, angle to hit the ball can also be considered


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Really looking forward to seeing this in action! You don't need a full arm - consider an inexpensive air hockey table and some RC servos for a proof of concept. In fact, have one at either end and have them play each other? Is the project finished or you plan to add more?

Hey, Thanks a lot. I will look into the Air Hocky for more details. 

No it is not finished. Currently I am trying to add additional parameters like trajectory, force etc. Once that is done, will plan to implement in hardware as you suggested.