getting started with your sense2gol

this guide will walk you through the setup and basic programming of your Sense2GoL. it consists of 4 sections:

  1. set up the environment by installing the necessary software and preparing your ide.
  2. write a simple program to make an led blink on your sense2gol.
  3. learn how to read raw data from your sense2gol.
  4. make use of your data and measure speed with your sense2GoL

1. setting up the environment

there are several ways to get your development environment set up, depending on your favorite ide. we’ve provided you with instructions for some common IDEs.

there are only two steps to get you ready to code:

  1. install Segger J-Link
  2. prepare your IDE

   1.1. installing segger j-link

in order to use and program the Infineon xmc microcontrollers, SEGGER J-Link must be installed on your pc. Please follow this link and install the j-link software and documentation pack for your operating system. if you have already installed ‘DAVE™ - Development Platform for XMC™ Microcontrollers‘, you can skip this step as the respective drivers/programs are already installed on your system.

     

     1.2 getting your IDE ready

1.2.1. arduino IDE

get the arduinoIDE from here. open it and select your board and port (under “tools → board” and “tools → port).

you can start coding, building, uploading and monitoring right away. make yourself familiar with it! (help can be found here)

1.2.2. platformIO

get platformIO from here. open it, create a new project and choose your board in the wizard.

the  platform.ini  file should look similar to this (depending on your board, here we used a Sense2GoL):

you can start coding, building, uploading and monitoring right away. make yourself familiar with it! (help can be found here)

2. making an led blink

to make an led blink on your sense2GoL, there are just two steps necessary: 

  1. set-up the board
  2. let the led blink

     2.1. set-up the board

for the   setup()  method, we only have to set the   LED1  pin to   OUTPUT  , so we can control the led:

     

     2.2 let the led blink

next, we want to light up the led in the  loop()  method.

this looks as follows:

 digitalWrite(LED1, HIGH)   turns the led on,   delay(500)   waits half a second before turning the led off again.

     2.3 watch it blink

upload your code to the sense2GoL and watch your led blink.

congrats! you just managed to talk to your board.

3. reading raw data

while a blinking led is cool, it’s not at all useful. let’s read some raw data from your sense2gol to get to the exciting things. 

three steps are necessary for this: 

  1. prepare the board
  2. read the data
  3. receive the data on your computer

      3.1. preparing the board

for the   setup()  method, there are two things we have to before being able to read out raw data.

3.1.1. opening a serial port

first, we need to initialize a serial port connection.

this is done by:

 Serial.begin(bitrate)   opens a serial connection and sets the bitrate for transmission via that connection. this transmission rate is also known as baud-rate and must match on the board side and on the computer side. for our sense2gol this rate is 9600 (for other boards you can find the rate in the documentation).

3.1.2. turning on the board

second, the sense2gol requires the  BGT_ON  pin to be turned on before it is possible to read data from the board.

this is done by:

in this case,  LOW   is enabled state. there is a slight difference when you have the sense2gol connected to another board. please see below for that.

otherwise, that’s it already for the setup.

3.1.3. connecting the sense2gol to another board

if you want to connect the sense2gol to another microcontroller, you have to activate the  BGT_ON  pin beforehand. this is done by uploading the following very simple script directly to the Sense2GoL:

after that, you can simply connect the   VCCIN  (5v power) and  GND  (ground) to the respective pins of your board and connect the data pins  IF_I_HG  and  IF_Q_HG  to some analog inputs of your board.

obviously, you don’t have to include the code to activate the  BGT_ON  pin anymore in your  setup()  method, because you did this by uploading the above script to the sense2gol.

now, you’re all set up for the loop.

     3.2. reading the data

next, we want to read the raw data in the  loop()  method. the board outputs the raw data on the  CH_I  (channel I) and the  CH_Q  (channel Q) pins (if you are not directly communicating with the sense2gol, replace with the respective analog input pins of your board). what we have to do is simply read those to pins and print their content to the serial port connection, so it appears on the serial console on our computer.

this looks as follows:

  analogRead(pin)  reads the raw data from a pin.   Serial.print(text)  and   Serial.println(text) send the raw data to the serial connection.

the whole code looks like this:

 

    3.3. receiving the data

if you compiled and uploaded the code to the board, it will read the raw data and send it to the serial connection. on your computer, you can see it arriving by opening a serial monitor (such as that from the ArduinoIDE).

the arriving data will look like this:

you should see the values changing when you’re moving the board or some object in front of it.

congrats! You just managed to read data from your radar sensor 

4. measuring speed

now, let’s leave the playground and get to some real project, making use of our data. did you always want to play the cop and measure the speed of cars? you’ve got the hardware to do that and here comes the code for your radar gun. 

three steps are necessary for this: 

  1. create a class for the Sense2Go, which is able to read the data
  2. implement a class for the data processing, especially for the FFT
  3. bootstrap the two classes and print speed

     4.1. sense2GoL class

let’s start with implementing a class for the sense2gol. So far, it doesn’t need to do much. mainly, we want to read the IQ data.

we’ll assume a header file defining the class and its functions. if you wanna check it out, you can do so here.

the implementation is quite simple:

the constructor just turns on the  BGT_ON  pin, which is necessary to read data (this is a peculiarity of the sense2gol).

the dispose function is empty so far. we could theoretically turn off the  BGT_ON  pin again…

this is the most important function for this part. we’re getting two buffers and read the IQ data into them. the delay and the length is determined by the  Sense2GoL  specs in the   _config .

     4.2. radardataprocessor class

that was easy. the  RadarDataProcessor  will be more complex. the header file again takes care of defining functions and variables.

this is the constructor:

we are going to use the   FFTAnalyzer   from the  DSP   library for computing the FFT. essentially, we’re initializing some variables in this snippet, such as the radar class  _radar   and the callback  cb . we’re also initializing the Hanning window.

the code for that looks like this:

what happens here is preparing the Hanning window values and storing them in   _hanningWindow[] . the length is determined by the    _radarFftSize .

next, we have a wrapper function for the sampling from the radar:

and another wrapper function for the FFT function. why we need those two wrapper functions, will become clear later on, when we’re scheduling those to tasks after each other.

the FFT is computed and the    _result   is passed to the callback function cb.

the core part is now the implementation of   computeFft() :

there are several steps here. first we sum up the I values to get the mean. next, the mean is subtracted from every data point and the   _hanningWindow   is applied.

the preprocessed data is passed to the   fix_fft()  function and the magnitudes for the first half of the  _radarFftSize  is computed by  compute_magnitude()  . it also returns the highest magnitude. since the FFT magnitudes are mirrored and we’re only interested in the absolute speed, not the direction, we can skip the second half of magnitudes.

if the maximum magnitude  _maxMagFreq.maq  exceeds the   fftThreshold , we’re converting it to velocity and store it in  _result.speed . below the threshold, the returned speed is 0.

     4.3. the rest

this was the hard part. all we have to do now is connecting the dots.

in our  main.ino  we’ll declare some variables:

the  setup()  function looks like this:

the serial connection is opened, the  Sense2GoL  initialized and passed to the  RadarDataProcessor  along with the  callback() .

now, we’re making use of the scheduler included in the Infineon XMC core. the  _interruptTimer  takes care of sampling some new IQ data from our  Sense2GoL . its interval is that of our radar, namely the  cycleTime . after the data is sampled, the   _interruptTimer  is started to process the fresh data.

 _timer  will execute the  algoTask() , which is our FFT, and stop its own execution afterwards

Lastly,  condition if displays the results, whenever there are new results  available  :

     done.

and that’s it! build and upload the whole code. then open the serial port from the console below and watch the speedometer display the speed of the things you move in front of your board. have fun playing!

5. door opener

     

     5.1 radardataprocessor class

   detectMotionWiththeRawData    is used to determine the direction of the movement based on the IQ  raw data.

motion direction is based on the phase shift, that's why in our  for  loop()   we are comparing I data with the Q data.

the  loop()  function displays the results, whenever there are new results  available  :

     done.

and that’s it! Build and upload the whole code. then open the Serial port from the console below and moving objects direction.

     5.2. See it at work

Here is your door opener. Choose your port and baudrate and hit start. Try approaching your door opening sensor and watch the doors open. It doesn’t open when you’re moving away from it!

6. presence detection

did you ask yourself what kind of algorithm is used in the smart street light? would you like to detect your presence using such an accurate solution? now you got the hardware, let's have some fun developing a solution.

     6.1 RadarDataProcessorClass

 presencedetection  is based on motions. this function calculates the average value of a raw data sequence.

     

      6.2 the rest

we classify motions as micro or a macro motion based on their magnitudes.

  • micro motion is used to detect the tiniest motion like breathing.
  • macro motion is used to avoid faults alarms.

adjusting parameters will make your radar more or less sensitive to one's presence.

the serial connection is opened, the  Sense2GoL  initialized and passed to the   RadarDataProcessor  along with the   callback() 

lastly, the  loop()  function displays the results , whenever there are new results  available  :

     Done.

and that’s it! Build and upload the whole code. then open the Serial port from the console below and detect someone's presence in front of your board. have fun playing!

done

that’s it! you’re all set up now and know how to program your sense2GoL. go ahead and learn what your radar chip is capable of by visiting the interactive radar tutorial

explore     develop    learn radar