Skip to content

Software

Ten000hours edited this page Apr 16, 2019 · 2 revisions

Software

The project uses the C++ language to program under the Linux system.

Pin control

Based on the hardware design, our team uses the wiringPi control pin.

#define Trig1 9 #define Echo1 8 #define Trig2 2 #define Echo2 0 #define Trig3 13 #define Echo3 12 #define Trig4 11 #define Echo4 10

void ultraInit(void) { pinMode(Echo1, INPUT); pinMode(Trig1, OUTPUT); pinMode(Echo2, INPUT); pinMode(Trig2, OUTPUT); pinMode(Echo3, INPUT); pinMode(Trig3, OUTPUT); pinMode(Echo4, INPUT); pinMode(Trig4, OUTPUT); }

Ultrasonic sensor (HC-SR04) ranging

  1. The Raspberry Pi sends a 10us pulse to the Trig pin.
  2. The HC-SR04 receives the pulse signal sent by the Raspberry Pi, starts transmitting ultrasoun, and sets Echo high. It is then ready to receive the returned ultrasound.
  3. When the HC-SR04 receives the returned returned ultrasound, set Echo low.
  4. Echo's high level duration is the time it takes for the ultrasound to travel from launch to return. Based on this time interval, the distance between the sensor and the object in front can be calculated.

dis = (float)(stop - start) / 1000000 * 34000 / 2;

Music player

Our team chose OMXPlayer as our music player.
Due to the unresolved issue of the Raspberry Pi assigned by our team (the default HDMI audio output cannot be modified to 3.5mm interface output). After many attempts, we found that we can use the following command to achieve audio output through the 3.5mm interface with OMXPlayer.
omxplayer -o local music.mp3

At the same time, OXMPlayer has a variety of shortcut keys.

Main logic

Our idea is to control the playing of different music by different distances between the hand and the ultrasonic sensor. The use of multiple ultrasonic sensors will mix a variety of music with different volume and performance speeds to create unique music.

One type of music is controlled by two ultrasonic sensors. The distance measured by one of the ultrasonic sensors is set to three intervals. The program will play the set three music between 5-15cm, 15-25cm and 25-35cm. Another ultrasonic sensor will be used to control the volume and playback speed of the three songs.

Subprocess

In order to achieve uninterrupted music for each group, the program needs to create a subprocess for each set of sensors. We use arun11299 library subprocess to implement a fast implementation of the subprocess. And through the communication between processes, we can use the send shortcut to implement the corresponding function for the OMXPlayer that is playing music.

auto p=Popen({"omxplayer","-o","local","--loop",path1},output{PIPE},input{PIPE}); // play the music
const char* msgq = "q";
p.send(msgq, strlen(msgq));

Self-starting

The project is dedicated to getting our products out of the bulky displays, keyboards and mice. Our products can be used directly by simply powering up and connecting your favorite speakers or headphones. So we need to make our program self-start after the Raspberry Pi is turned on.

sudo nano /etc/rc.local

Write script path:
/home/pi/DJmixer/clientStart.sh start

UML

Clone this wiki locally