You are on page 1of 23

PROJECT REPORT

Batch -F

TOPIC – GAME DEVELOPMENT

GAME NAME – Dancing Target Shooting Game

Group Members -

 Abhishek Goyal (150101002)


 Rohan Aggarwal (150101052)
 Roopansh Bansal (150101053)
 Surabhi Gupta (150101078)
GAME IDEA
 The game consists of three targets which are to be shot
by the player using a laser gun as early as possible.
Two of the targets are stationary and the third target is
moving.

 The stationary targets being shot are indicated by their


falling down while when the moving target is hit it stops
moving. A haptic response on the player’s hand is also
sent.

 At a moment the player can only hit a particular target


which is specified by the GUI.

 To make things difficult for the player he also has to


move in specific compartments also specified by GUI
and only when he is in the right compartment is he able
to shoot the targets.

 Further to make things interesting the moving target is


being controlled by a second player through MQTT
whose aim is to delay the shooting as much as
possible.

 At the end of the game (all targets shot) a buzzer beeps


and the player’s score is calculated and displayed on a
remote leader board through MQTT.
Game Setup
1) Dancing Setup on Ground

The following image shows the setup of the dancing game


on the ground. It contains 3 ultrasonic sensors each of
which is triggered one after the other in regular intervals.
The sound starts travelling and comes back after hitting
some object. The time for the sound to come back can be
used to calculate the distance of the nearest object in that
compartment.

The compartment having the least distance must have the


player standing in it.

This is matched with the value displayed on the GUI and


only then the player is allowed to shoot.

All this work is done by a separate parallel thread which


maintains the value of the compartment the user is in.
2) Fixed Targets

There are two fixed targets each having a light sensor in the
centre and a servo motor attached at the bottom. The duty
cycle of the servo motor is used to control at which angle it
is currently at. When the target is shot the duty cycle is
changed to make the target fall down.
The light sensor values are being fed to a parallel thread
which confirms whether the value is greater than the
threshold which will happen when the person is shooting in
the sensor.
This thread also matches the value of the box person is in
and the box he is supposed to be in.

If all conditions are true then this thread would handle the
servo motors in such a way that they drop down. Further it
will never check these light sensors again.

3) Gun with Haptic Sensor

The Gun is the central part of the game and is what the user
uses to shoot the target. It has a laser attached on its front
which is used to hit the targets and also a haptic sensor
attached at the back.
Whenever any target is shot down a Haptic Response is
used to indicate to the player that the target has been shot.

4) Moving Target

The third and the most difficult target to hit is the moving
one. The target is being moved using two DC motors and a
thread wound on them. The two DC motors move in
opposite direction for the correct unwinding and winding of
the thread.

The important feature is that the motors are being controlled


by a second person through MQTT. Any player can connect
to the broker and act as a second player to control the
motor.

Again a thread is used to check the light sensor values and


the other conditions for fulfilment of the shooting. When all
these conditions are satisfied the target is shot and a MQTT
message is sent to the code controlling the motors which
stops all further motion of the motors.

5) GUI

The GUI is implemented for the person playing the game


and tells us two things.

The bigger of the boxes indicates which block the person


should move so that he is standing in the right box and can
shoot the targets.
The smaller box indicates which of the light detecting
sensors is currently active which the person can shoot.

6) MQTT

A MQTT server is set up using MOSQUITO as the broker


while using PAHO MQTT as the MQTT library for Python.

We have set 3 remote desktops one of which act as both


Publisher and Subscriber, one which acts as only
Subscriber and one which acts as only Publisher.

We now explain them as follows -

1) Remote Desktop for Opponent, Running on topic 1


(Publisher Only)

Its job is to receive input from the user and send in the
direction continuously to the Remote Desktop on the Pi.
2) Remote Desktop for Leader Board, Running on topic 2
(Subscriber Only)

Its job is to receive input from the code running on the


Remote Desktop on the Pi and update the leader board.

3) Remote Desktop on Pi, Running on both Topic 1, Topic 2


(Both Subscriber and Publisher)

Till the light detect sensor of the moving target is hit and
Topic 1 receives the message to end the movement, it acts
as a subscriber continuously receiving messages on Topic 1
and moving the motors accordingly.

After the light detect sensor is hit, the code waits for the
main program to get over and sends the time of the user to
leader board remote desktop.
SENSORS

ULTRASONIC PROXIMITY SENSOR

Ultrasonic proximity sensors are electronic devices that sends out a


high-frequency sound pulse beyond the upper range of human
hearing (which is between 20 hertz and 20 kilohertz) and determine
the distance between the sensor and an object based on the time it
takes to send the signal and receive the echo.
The speed of sound is approximately 340m/s in air. The ultrasonic
proximity sensor uses this information along with the time difference
between sending and receiving the sound pulse to determine the
distance to an object. It uses the following mathematical equation:

Distance = (Time x Speed of Sound)/2

Time = the time between when an ultrasonic wave is transmitted


and
when it
is
receiv
ed
Physics behind the generation of Ultrasound:
Ultrasound is most commonly generated as a direct conversion
from electrical energy. This is accomplished by applying a rapidly
oscillating electrical signal to a piezoelectric crystal attached to a
mounting. The charge causes the crystal to expand and contract
with the voltage, thereby generating an acoustic wave. The waves
are later detected by a piezoelectric receiver, which converts the
waves back into voltage using the same method
To generate the ultrasonic wave. The ultrasonic pulses thus
generated travel in a cone-shaped beam.
The range of an ultrasonic sensor is determined by the frequency of
vibration of the crystal. As the frequency increases, the sound
waves transmit for progressively shorter distances. Conversely, as
the frequency decreases, the sound waves transmit for
progressively longer distances. Thus, long-range ultrasonic sensors
work best at lower frequencies, and short-range ultrasonic sensors
work best at higher frequencies
DIGITAL LIGHT SENSOR

Digital light sensor uses the I2C light-to-digital converter (TSL2561)


to transform light intensity to a digital signal. It has two light
sensitive diodes: infrared and full spectrum, thus allowing it to
separately measure infrared, full-spectrum or human visible light.
Physics behind the working of photodiodes:
A photodiode is basically a semiconductor device that converts light
into an electrical current.
The current is generated when photons are absorbed in the
photodiode. Photodiodes are exposed (to detect vacuum UV or X-
rays) or packaged with a window or optical fibre connection to allow
light to reach the sensitive part of the device. A photodiode is
designed to operate in reverse bias.
A photodiode is a p-n junction or PIN structure. When a photon of
sufficient energy strikes the diode, it creates an electron-hole pair. This
mechanism is also known as the inner photoelectric effect. If the
absorption occurs in the junction's depletion region, or one diffusion length
away from it, these carriers are swept from the junction by the built-in
electric field of the depletion region. Thus holes move toward the anode,
and electrons toward the cathode, and a photocurrent is produced. The total
current through the photodiode is the sum of the dark current (current that
is generated in the absence of light) and the photocurrent, so the dark
current must be minimized to maximize the sensitivity of the device.
This electric current thus generated is converted to digital by the built in
analog to digital converter of the grove digital light sensor.

I-V characteristic of a photodiode


HAPTIC SENSOR (VIBRATION)

A vibrating motor uses a small unbalanced mass on a DC motor, which


when rotates, creates a force on the mass that translates to vibrations,
which are felt by the person holding in the Dancing Laser Shooting game.

DC MOTOR

Motors convert
electrical energy into
mechanical energy (used to cause rotation).

A DC motor has six basic parts -- axle, rotor (armature), stator,


commutator, field magnet(s), and brushes. In most common DC motors,
the external magnetic field is produced by high-strength permanent
magnets. The stator is the stationary part of the motor -- this includes the
motor casing, as well as two or more permanent magnet pole pieces. The
rotor (together with the axle and attached commutator) rotate with respect
to the stator. The rotor consists of windings (generally on a core), the
windings being electrically connected to the commutator

Physics behind the working of a DC motor:


Operation of a DC motor is based on electromagnetism. A current carrying
conductor generates a magnetic field; when this is then placed in an
external magnetic field, it will experience a force proportional to the
current in the conductor, and to the strength of the external magnetic field.

The size of the force, which determines how fast the motor spins, depends
on the following:
a) The amount of current in the wire,
b) The length of the wire
c) The strength of the magnetic field
Force = (current) x (wire length) x (magnetic field)

The direction of the force, which determines which direction the motor
spins, depends on the following:
a) The direction of the current in the wire
b) The direction of the magnetic field

The Right Hand Rule is used to


determine the direction of the force
when the direction of the current and
the direction of the magnetic field are
known.
SERVO MOTOR
A servomotor is a rotary actuator or
linear actuator that allows for precise
control of angular or linear position,
velocity and acceleration. It consists
of a suitable motor coupled to a
sensor for position feedback. It also
requires a relatively sophisticated
controller, often a dedicated module
designed specifically for use with
servomotors.

Physics behind the working of a Servo motor:


Say at initial position of servo motor shaft, the position of the
potentiometer knob is such that there is no electrical signal
generated at the output port of the potentiometer. This output port of
the potentiometer is connected with one of the input terminals of the
error detector amplifier. Now an electrical signal is given to another
input terminal of the error detector amplifier. Now difference
between these two signals, one comes from potentiometer and
another comes from external source, will be amplified in the error
detector amplifier and feeds the DC motor. This amplified error
signal acts as the input power of the DC motor and the motor starts
rotating in desired direction. As the motor shaft progresses the
potentiometer knob also rotates as it is coupled with motor shaft
with help of gear arrangement. As the position of the potentiometer
knob changes there will be an electrical signal produced at the
potentiometer port. As the angular position of the potentiometer
knob progresses the output or feedback signal increases. After
desired angular position of motor shaft the potentiometer knob is
reaches at such position the electrical signal generated in the
potentiometer becomes same as of external electrical signal given
to amplifier. At this condition, there will be no output signal from the
amplifier to the motor input as there is no difference between
external applied signal and the signal generated at potentiometer.
As the input signal to the motor is nil at that position, the motor
stops rotating.
This is how a simple conceptual servo motor works.

In our project, we controlled the servo by the pulse width of a 50 Hz


(20 milliseconds time period) PWM signal.
The position to which it moves to depends on the pulse width of the
signal.
Typically, the servo will go to the full left position when it sees a
pulse width of 1 millisecond, it will go the middle position when it
sees a pulse width of 1.5 millisecond, and it will go to the full right
position when it sees a pulse width of 2 millisecond. However, that
on the Raspberry Pi we do not specify a pulse width, but we specify
a Duty Cycle. So, we use the following relationship:
Duty Cycle =Pulse Width/Period.
SALIENT FEATURES OF CODE

Parallelism

There are 4 parallel threads working simultaneously in the backend


code including the main thread.

Their works are as follows: -


 T1 – Pings all the ultrasonic sensors at regular intervals and
decides in which box is the player currently standing
 T2 – Controls the background movement of the motor using
MQTT
 T3 – Decides randomly at regular intervals which box the
player should be in.
 T4 – The main thread whose main job is to ping the light
sensor values and tell us if some target is hit
Ultrasonic Sensor

As explained above the ultrasonic sensor works by sending out the


ultrasonic signal and waiting for its return. This is calculated in the
while loop as shown and then the distance is calculated by
multiplying it with the speed of sound.
MQTT

As mentioned before there are three MQTT remote desktops each


of whose code is explained below

1) Remote Desktop for Opponent, Running on topic 1


(Publisher Only)

As the code describes we first make a connection with the


broker and then send whatever input we get to the broker
subscribed to topic/test.
2) Remote Desktop for Leader Board, Running on topic 2
(Subscriber Only)

As the code describes we make a connection with the


broker and wait forever for messages on topic/test2.
We run the function on message and it take the value of the
player’s score and updates and displays the new leader
board.
3) Remote Desktop on Pi, Running on both Topic 1, Topic 2
(Both Subscriber and Publisher)

We first connect to the broker and listen to all messages on


topic/test and in adjust the motor pins accordingly. If the game has
ended we break the connection and reconnect to the broker and
publish now our score to topic/test2.

You might also like