Sanor sensors readings.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

Hello Everyone, I need some tips for my project..

   

 

   

I am trying to read data from 4 sonar sensors, after that, i want to take the max value of all of them and average them.

   

Thanks.

0 Likes
39 Replies
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Some questions -

   

 

   

1) Do you know how a sonar sensor works ?

   

2) How to measure time with PSOC ?

   

3) Are you new to PSOC and its design tool Creator ?

   

4) Is the sonar sensor signal conditioned or a "raw" sensor ? By this

   

I mean does it put out pulse width or voltage or current proportional to distance,

   

or just a small signal when it receives sound pressure ?

   

5) Part number of sonar sensor.

   

6) Are you working with a Cypress development board, if so which one ?

   

7) Goals of design, distance range you want, resolution in meters or feet, accuracy......

   

😎 Medium sensor is transmitting in, air, water, fluid.......

   

 

   

This will give forum some idea of how to help you.

   

 

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

1) Do you know how a sonar sensor works ?

   

Yes. it send a pulse and when it hits something it bounce back..

   

 

   

2) How to measure time with PSOC ?

   

I do not know, sorry.

   

 

   

3) Are you new to PSOC and its design tool Creator ?

   

yes, i am new to PSOC..

   

 

   

4) Is the sonar sensor signal conditioned or a "raw" sensor ? By this

   

I mean does it put out pulse width or voltage or current proportional to distance,

   

or just a small signal when it receives sound pressure ?

   

it put out a voltage proportional to distance.

   

 

   

5) Part number of sonar sensor.

   

this is the website for the sonar sensor.. ( http://www.robotshop.com/en/maxbotix-ez1-ultrasonic-ranger.html?utm_source=google&utm_medium=base&ut... )

   

 

   

6) Are you working with a Cypress development board, if so which one ?

   

we are using PSOC5 LP and also we are using PSOC creator 2.1

   

 

   

7) Goals of design, distance range you want, resolution in meters or feet, accuracy......

   

There are going to be 2 robots. the sonar sensors will prevent the second robot to hit the first one and to avoid going too far. If it goes too far, the robot will speed up. if it goes too close, the motor will slow down.  it should keep a distance of 5 feet.

   

It will keep sampling every 400ms. after that, we want to take the max value of each one and average them.. the value will be plug into a equation. the result will be compare with a table to make sure the robot is withing the safe distance.

   

 

   

😎 Medium sensor is transmitting in, air, water, fluid.......

   

it is in Air

   

 

   

I will attached a picture to show you what we have so far.. any tips or example that can help us??

0 Likes
Anonymous
Not applicable

This is a new picture of what we think it is the final result...

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Thanks for answering.

   

 

   

First i recommend you download and install Creator 3.0, its free,and fixes bugs

   

in previous versions.                  http://www.cypress.com/psoccreator/

   

 

   

The basic approach you would use is convert the voltage to a digital value, then

   

apply your software tests to calculated distance.

   

 

   

The sensors are measured one at a time, via SW control of the mux. You can either

   

poll the A/D or you can set up an interrupt when it finishes a conversion. When you change

   

mux channel you need to delay to allow mux voltage to settle to the next sensor, that delay

   

is shown in datasheet. Then I would throw one A/D sample away (ignore it) and use the

   

next sample for data to use in calculations.

   

 

   

There are videos that train you on use of Creator here -    

   

          http://video.cypress.com/video-library/video/PSoC-Software

   

 

   

Concentrate on 2012 videos, early part of 2013.

   

 

   

Regards, Dana.

   

 

   

0 Likes
Anonymous
Not applicable

 Good day everybody,

   

 

   

I am in the same project group as the previous poster (Zeri).  I will see if I can expand upon what has already been stated;

   

 

   

1) We have 4 sonar sensors that return various voltage signals, with the maximum voltage output being the closest to the manufacturer's rating of voltage per foot.  So, for example, we may be 2 feet away, but we are getting voltage value jumps from 1mv to 200 mv (randomly).  The correct output is near 200 mv.   So my thought was this:

   

Freezing analog inputs and storing the maximum value for the half second and then outputting would work well (In theory). Do correct me if I am wrong.  

   

 

   

First, we poll the sensors for half a second, and pick the maximum value from that half second and use that in our distance formula.

   

 

   

Next, that value from the previous step will be passed on to, presumably, the PWM to adjust the motor's speed if and when necessary.  I think this may also be an interrupt, but I am not too sure on how to set up interrupts on the PSoC 5LP.

   

 

   

We are using DC motors, with sabertooth motor controllers:

   

http://www.lynxmotion.com/c-10-motor-controllers.aspx

   

The top one is our model in our robot.

   

Our motor model is:

   

http://www.lynxmotion.com/p-653-gear-head-motor-12vdc-301-200rpm-6mm-shaft.aspx

   

I have been looking for instructions on controlling geared DC motors with psoc but found none.  Only the stepper motor models.

   

Thank you for any help you provide on getting these up and running.  

   

 

   

I saw your MUX -> ADC converter diagram.  One problem I see with that, again, from an untrained PSoC user, is that there will be a lot of inputs during our sampling rate, and the 4 input MUX would not be able to handle it (again, from an untrained user).  Am I missing something from the diagram?  Thank you for any help you can provide

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Freezing analog inputs and storing the maximum value for the half second and then outputting would

   

work well (In theory). Do correct me if I am wrong.  

   

 First, we poll the sensors for half a second, and pick the maximum value from that half second and use

   

that in our distance formula.

   

 

   

1) What causes the noise level, other objects moving in the field of the sensor ? You could approach it just the

   

way you are now, you could average out the noise, or do a peak detection scheme either in analog or just

   

testing a stream of samples. Or use DFB block and filter.

   

 

   

    

   

   

          

   

Next, that value from the previous step will be passed on to, presumably, the PWM to adjust the motor's

   

speed if and when necessary.  I think this may also be an interrupt, but I am not too sure on how to set up

   

interrupts on the PSoC 5LP.

   

 

   

2) Interrupts, attach an ISR component to the HW source of interrupt. Name it. Tool then creates a .c and .h

   

placeholder files to put your code and definitions in. Global interrupt must be enabled. I do not think you

   

need an isr to feed the pwm, just take your decision output of sensors and write to PWM to update it.

   

 

   

    

   

   

           I saw your MUX -> ADC converter diagram.  One problem I see with that, again, from an

   

untrained PSoC user, is that there will be a lot of inputs during our sampling rate, and the 4

   

input MUX would not be able to handle it (again, from an untrained user).  Am I missing

   

something from the diagram? 

   

 

   

3) The A/D primarily sets sample rate. The settling time of the mux is the following, so I am

   

not sure I understand your question -

   

 

   

0 Likes
Anonymous
Not applicable

 According to the spec. There are 232 output and PW output. It may be easier to use those outputs. using interrupt would solve your problem, no need to sample the analog value.

0 Likes
Anonymous
Not applicable

You mentioned there are 4 sensors, were they pointing to the same direction? Are they being controled or free running? Are there consideration for the echo from an object from the transimmision of another sensor causing incorrect readings?

0 Likes
Anonymous
Not applicable

 according to the specfication of the motor controller, you can use ananlog value or use serial signal to control. the only issues is you need to use servo control (PID) algorithm to adjust the speed of the motor.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Or use an unconditioned sensor, one w/o the PIC and analog on the module,

   

and save a lot of cost ? Use PSOC for all the conditioning + the heavy lifting,

   

product functionality.....Just a thought as the sensor shows a cost of ~ $25.

   

Why have two processors when one PSOC will do ?

   

 

   

Notice PIC on Sonar module does not have internal reference, uses Vdd for

   

reference, and unless Vdd is itself a ref accuracy/repeatability part to part is

   

.......?

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Actually on reflection it would be 1 PSOC vs 1 PSOC + 4 pic based sensors

   

at $ 25 each. Ech.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The of course one can look at replacing the $ 50 - $ 75 motor controller

   

with the PSOC + a motor driver ( ~ $ 5 or less ) chip to handle motor currents.

   

 

   

PSOC, conquering the world one UDB at a time.....

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 Replacing the sensors and the motor control with a PSoC is possible but would increase the complexity of the project. I think the sensor part would be best to use the off the shelf one, but including the motor control would not be too hard as you would need to do the servo control part anyway. 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Cases

   

 

   

1) 1 PSOC + 4 PICs + 4 OpAmps + 1 Motor Control processor board = ~ + $ 150 additional

   

product cost, reduced reliability, larger size, more power, lower software design, less competitive.

   

 

   

2) 1 PSOC + 1 motor driver part = - $ 25 product material cost, increased reliability, smaller size,

   

less power, increased software design, more competitive.

   

 

   

If you are making 5 of these, go for 1), if 5000, go for 2).

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

 You state that you use the maximum reading, what is the reason for that?

0 Likes
Anonymous
Not applicable

Our biggest problem right now is our motor controllers take analog input and we are trying to manipulate theamplitude of voltage signals sent to them.Iis there a way to manupulate the amplitues of analog signals that is being sent to the motor controller from the PSoC without an external RC circuit?

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

There are several ways to generate analog voltages -

   

 

   

1) VDAC or IDAC

   

2) PWM (needs external RC to filter out ripple)

   

3) WaveDAC8 (has a VDAC in it) followed by S/H

   

4) Ramp generator with S/H, former requires external cap, IDAC

   

could drive integrator or just external cap.

   

 

   

What is the requirement ?

   

 

   

a) Resolution

   

b) Range

   

c) Slew rate

   

d) Accuracy

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

DAC is the easiest way to send analog signal out. just remember that zero point to the controller is 2.5V, and you need to design a servo loop to control the speed of the motor.

0 Likes
Anonymous
Not applicable

 Hello again,

   

 

   

Thank you for your help so far.  I have a few more questions for you this time around.

   

 

   

For our 4 sonar sensors, I created 4 analog input pins.  I then connected them to the MUX.   Do I need to read and write the incoming values in the input pins before the MUX?

   

 

   

Once they are at the MUX, do I read or write there? Or does the value pass to the ADC converter? 

   

 

   

Once the values are at the ADC converter, and given that the above questions are answered, how do i convert the value, and store it in an array? 

   

 

   

The above questions really are out of not really understanding the PSoC.  I see some function calls in the datasheets, I am just not sure if using them is a requirement or not.

   

 

   

The end goal, is to read 4 sonar sensors, take the values from them and pick the maximum value over a period of time.  That value will then be plugged into a distance equation and an adjustment speed signal (interrupt?) will then be sent to the UART for the motor controllers to send out new RPM value.

   

 

   

Thank you kindly for your help!

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

For our 4 sonar sensors, I created 4 analog input pins.  I then connected them to the MUX.   Do I need to read and write the incoming values in the input pins before the MUX?

   

 

   

If you are using voltage mode then you will command the analog mux to select a specific channel for A/D conversion.

   

If you are using pulse width mode, then using a digital mux you would feed that to a counter to measure the PW.

   

 

   

Once they are at the MUX, do I read or write there? Or does the value pass to the ADC converter? 

   


   

Pass to A/D converter if using voltage mode.

   

 

   

Once the values are at the ADC converter, and given that the above questions are answered, how do i convert the value, and store it in an array? 

   

 

   

You test A/D for end of conversion, then read it, and write to an array element, like result[ i ], then increment i

   

to point to the next array element for the next write.

   

 

   

int16 result[ 100 ];      // Declare the array

   

uint8 i = 0;                    // Pointer to array element to work on

   

 

   

                                      // Here do test for EOC of A/D using A/D EOC test API

   

result[ i ] =                   // Read A/D using A/D read API

   

i++;                               // Get ready for next sample

   

 

   

                                      // Do a loop on this until you have acquired all the samples you need. Then

   

                                      // start i back = 0 to do it again after you proecess the array.

   

 

   

The above questions really are out of not really understanding the PSoC.  I see some function calls in the datasheets, I am just not sure if using them is a requirement or not.

   


   

Yes, you use the module APIs to control it, like mux, A/D. To start it, config it, and use it.

   

 

   

Regards, Dana.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

I forgot to address this " how do i convert the value, "

   

 

   

The sensor datasheet must give you a relationship between voltage (pulse width if you use

   

that mode) and distance. So you convert A/D reading to voltage (or use API that does that) and

   

then use that in equation in datasheet. You can use either floating point variables if you have

   

fractional data or interger arithmetic depending on resolution and conversion equation.

   

 

   

Basically the conversion eqaution is ( Vcc sensor / 512 ) Volts per inch. According to datasheet.

   

So if Vcc is 5V, then conversion factor is 9.77 mV / inch.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

From the AMUX data sheet, you can see the API list.

   

use the AMux_Select,  AMux_FastSelect, or the  AMux_connect to connect the ADC to the analog signal you want.

   

I normally use the  AMux_select. 

   

Once selected the ananlog input, then start the ADC convertion, at the end of the conversion, read the data to an array to process later. 

   

(You can process the distance immediately, but I would prefer to do all in one go after finish reading all channel.

0 Likes
Anonymous
Not applicable

 Hello again,

   

 

   

I am still unsure of a couple things with the sensor, mux and ADC.

   

 

   

1st question) To read the input from the sonar sensors, I need to use something like this, correct?:

   

 

   

uint8 values;

   

values= PIN_1_Read();

   

Is this correct?

   

 

   

2nd Question) To have the Analog MUX read the values from all 4 analog sensors into an array over a set-time

   

I first need to intialize the mux 

   

using the void_AMUX_init() , right?

   

then start it?

   

then connect and then disconnect after every input is read? 

   

I guess I am stuck on how to actually have the MUX read the values from the 4 sensors into an array

   

Also, do I need a timer somewhere to have this take place over a set-period of time (most likely, less than 1/4 second)

   

 

   

3rd  Question) The only reason the 4 sensor inputs are read is for the distance equation.  Once we select the maximum value from the 1/4 second reading from the sensors through the  MUX, we plug that value into a programmed equation (the best fit line) to find the distance.  Do we still need the AD Converter? My advisor seems to not think so anymore, given our use of the sensors.

   

 

   

As always, Thank you so much for your help!

0 Likes
Anonymous
Not applicable

Some thing like this

   

 

   

**

   

 uint16 uiData[4];

   

 AMUX_Start();
 ADC_Start();

   

 /* For each sensor */
 for (ucChannelNumber = 0; ucChannelNumber < 4; ucChannelNumber++)
 {
  
  /* Select the channel */
  AMUX_Select(ucChannelNumber);
  
  /* Start the AD conversion */
  ADC_StartConvert();
 
  /* Wait until conversion complete  */
  ADC_IsEndConversion(ADC_WAIT_FOR_RESULT); 

   

  uiData[ucChannelNumber] = ADC_GetResult16();

   

 }
 
 /* Stop the ADC */
 ADC_StopConvert();

   

 /* Now the ucData[] has the analog value */

   

/* select the max. and do the conversion and put into your formula */

0 Likes
Anonymous
Not applicable

The above is for the the sensor reading.

   

You need to have a timer which gives an interrupt every 250mS and set a flag to start your conversion.

   

I think it would be easy to get examples for timer operation.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The sensor you are using has 3 different methods of interface -

   

 

   

1) Voltage proportional to distance         (you use the A/D to measure)

   

2) Pulse width proportional to distance (you would uses a timer to measure)

   

3) Digital RS232                                         (you would use UART to read)

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello again,

   

 

   

Here is our code with our progress so far.  We are having NO LUCK at all! We still do not understand the correct order of how to initialize everything and to start and stop sensor readings, and ADC conversions and getting it to work with our code. 

   

 

   

We have bypased a MUX for the time being.  We just want to get one of the sonar sensors working first before attempting to add other ones in.   We have our analog input going directly to our ADC converter. 

   

 

   

Thank you!

0 Likes
Anonymous
Not applicable

Hi Zeri,

   

Have you build your project under creator?

   

1. when you call a function you do not include the return type of the function.

   

2. you only need to start the ADC once, no need to put it inside the loop. Do it at the beginning of your main routine.

   

3. there is no ADC_GetResult() but ADC_GetResult8() or ADC_GetResult16() - you should be able to find that in the data sheet.

   

4. The   ADC_GetResult16()  return a 16bit data, to store that in a local variable use

   

  uint16 data = 0;

   

data = ADC_GetResult16() ;

   

to store in a array;

   

uint16 data[4];

   

data[0] = ADC_GetResult16() ; /* store this in the first element of an array */

   

5. I normally declare the variables at the beginning of a function although C++ allows declaring variable anywhere inside the function.

   

6. If you are using creator with minipro3, you can just run the problem with a loop reading the data first and break at the end of the loop to check for the value.

   

7. Functions to be called inside a function should have function prototype declared first.

   

8. What is sonar_read()? and sonar_write()?

   

9. May be you should spend some time with some simple C program and PSOC examples first.

0 Likes
Anonymous
Not applicable

Sorry there is some type

   

**

   

int16 data = 0;

   

data = ADC_GetResult16() ;

   

to store in a array;

   

int16 data[4];

   

data[0] = ADC_GetResult16() ;/* stroe the result in the first element of the array*/

   

**

   

WE NEED AN EDIT FUNCTION!!!!! PLEASE.

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

This does not use array -

   

 

   

    

   

          

   

This is approximate –

   

 

   

int SonarMaxValue( void );                                                          // Function prototype

   

.

   

.

   

int MaxValue ;                                                                                   // Maximum value (distance) found in 100 samples of distance measured

   

ADC1_Start( );                                                                                   // Initialize A/D

   

ADC1_StartConvert( );                                                                   // Start A/D converting

   

.

   

.

   

.

   

int SonarMaxValue( void ) {                                                         // Function to extract max value from 100 samples

   

 

   

MaxValue = 0;                                                                   // Reset MaxValue

   

for ( int8 I = 0; i < 100; i++ ) {

   

 

   

                ADC1_IsEndConversion(  ADC1_WAIT_FOR_RESULT );                   // Wait until A/D finished converting

   

PossibleMaxValue  = ADC1_GetResult16( );                                         // Get the latest A/D sample

   

If (PossibleMaxValue > MaxValue ) MaxValue = PossibleMaxValue;

   

 

   

}

   

 

   

Return MaxValue;

   

}

0 Likes
Anonymous
Not applicable

@Dana,

   

The array is for 4 sensor channels not for averaging.

   

@zeri,

   

I still has concern with your design

   

1. you have 4 sensor which are free running whiche uses echo from object to determine the distance. Have you consider the interference between the sensor as they may trasnimitt at slightly different time and the echo receive from one sensor may be due to the other sensor which makes the reading too short. So you cannot get a accurate reading? Did you consider controlling the sensor by the PSoC?

   

2. Is there any reason why you cannot/don't use the serial output (or the PWM output) from the sensor as this would eliminate all those AD conversion issues. It is easier to handle it in the digital domain in this application.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hello again, just some more questions.  We are trying to get the mux on board working.  We are having great difficulty, please do help.  You always provide us with excellent knowledge, and we look forward to hearing back from you

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Please define "get working". What did you try, what do you expect, what happens instead?

   

I suppose you already read the relevant data sheets and AppNotes, regarding the usage of ADC and Mux together? Did you read esr?

0 Likes
Anonymous
Not applicable

My apologies, let me restate my question

   

We brought in a PSoC expert to help us out, but he was not sure how to navigate the MUX readings. 

   

We have four sonar sensors connected to the PSOC 5LP, connected via AMUX to a DelSig ADC Coverter.  Our reading is not even cycling through the MUX to get readings.

   

Is there a way to first, change the speed of the MUX? Im going into configure mode and there is no visible option.  Im thinking the samplng rate of the MUX may be too high, to where the sonar sensor doesnt have time to get a reading and send it to the ADC.  Another idea is that we are missing something altogether. 

   

Secondly, does the code accurately represent what might be a successful MUX for 4 inputs (located on PINS [0,0] [0,1] [3,6] [3,7]?

   

Thirdly, is there even a reason to use a MUX? Say if I connect 2 input directly to a ADC, is there a way to temporarily disable one input while reading the other?

   

Thank you for any help you can provide

0 Likes
Anonymous
Not applicable

The mux is totally controlled by you, by using the APIs in the datasheet. Thats how you

   

go from one channel to the next.

   

 

   

Are you observing thesetiming considerations (sensor datasheet) -

   

 

   

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Image did not paste, attached.

0 Likes
HeLi_263931
Level 8
Level 8
100 solutions authored 50 solutions authored 25 solutions authored

Still, what is your question? You are not cycling through the mux, so you will always get the results from the first channel.

   

A mux has no 'speed'. It will select another input when you tell it to. it also has no sampling rate, its just a collection of analog switches. But you need to ensure that the DelSig ADC won't get confused when you switch its input signals. Best is to set it to single conversion mode, and start the conversion after the mux has been switched to another channel. (Read thr DelSig ADC part on conversion mode for more info on that).

   

Regarding your code: the first for loop is not needed - you then convert all four channels with the inner loop, and repeat this four times, overwriting the old resultst. Why?

   

To your last question: exactly that is what a mux is for - disabling the inputs.

0 Likes
Anonymous
Not applicable

When you switch a mux, you have to allow for settling time due primariliy

   

to R & C effects, time can be pronouced when running a DelSig at hi res

   

trying to settle to 1 LSB (as an example).

   

 

   

You can stop / start converting as a result of mux change, or allow it to run

   

continuously and throw away 3 or more samples after a mux change. 3 required

   

by DelSig Filter latency time.

   

 

   

    

   

          http://www.cypress.com/?app=forum&id=2233&rID=83590

   

 

   

Regards, Dana.

0 Likes