Forum.

 

Need help or have a question? Join the conversation in our community topics and share your ideas, tips, or challenges—we’d love to hear from you! Before posting, be sure to check our

 

?

Multiple sensors at...
 
Notifications
Clear all

[Solved] Multiple sensors at the same time - block programming

18 Posts
4 Users
6 Reactions
16.5 K Views
Balázs Szandavári
(@abacusan)
Active Member
Joined: 2 years ago
Posts: 6
Topic starter  

I would like to use several sensors at the same time. For example, line tracking until an obstacle is detected. However, line tracking does not work as soon as I insert the ultrasonic sensor.



   
Quote
Ivan Artiles
(@ivanr3d)
Active Member
Joined: 3 years ago
Posts: 13
 

Posted by: @abacusan
↑

I would like to use several sensors at the same time. For example, line tracking until an obstacle is detected. However, line tracking does not work as soon as I insert the ultrasonic sensor.

It is possible if you recreate the code block by block. For instance, if you want to track lines while avoiding obstacle, you can recreate the line following code and add the ultrasonic reading where you want. I am attaching a version of a line following made block by block, you can find it in the documentation examples of webcode. 



   
ReplyQuote
Balázs Szandavári
(@abacusan)
Active Member
Joined: 2 years ago
Posts: 6
Topic starter  

This is the program. But it does neither of these tasks.



   
ReplyQuote
Camilo
(@cparrapa)
Developer Admin
Joined: 3 years ago
Posts: 112
 

Hi @abacusan

Thanks for posting about this, we are checking if is a hardware limitation or bug in the code but that should work. I am testing it and there is some sort of conflict between the 2 sensors.

Can you confirm if your sensors work separately fine?



   
ReplyQuote
Balázs Szandavári
(@abacusan)
Active Member
Joined: 2 years ago
Posts: 6
Topic starter  

Yes. The line tracker ir photoreflectoros and the ultrasonic senor are working separetely too. Together in one program just one. Which sensing something first.



   
Camilo reacted
ReplyQuote
Balázs Szandavári
(@abacusan)
Active Member
Joined: 2 years ago
Posts: 6
Topic starter  

It is probably the ultrasound's fault, because it can execute a complex program with a different sensor (in the video, another robot's (ArTeC) touch sensor). In the video: it follows a line until the button is pressed.



   
Camilo reacted
ReplyQuote
Balázs Szandavári
(@abacusan)
Active Member
Joined: 2 years ago
Posts: 6
Topic starter  

The video



   
Camilo reacted
ReplyQuote
Ivan Artiles
(@ivanr3d)
Active Member
Joined: 3 years ago
Posts: 13
 

Hey @abacusan very interesting! So it's mean there is something wrong in the interaction between the line sensor and the ultrasound one. Definitely something to research about. 



   
ReplyQuote
Camilo
(@cparrapa)
Developer Admin
Joined: 3 years ago
Posts: 112
 

guys @abacusan @ivanr3d

this might be a solution Ultrasonic Sensor not working? – Blocks – HP robots Forum

@alex-just-alex you also made a code that works could you share?



   
ReplyQuote
Alex Just-Alex
(@alex-just-alex)
Active Member
Joined: 3 years ago
Posts: 8
 

sure it's quite crude, really just a proof of concept and is written in micropython. Here is a brief video and below,the code

 



   
Camilo reacted
ReplyQuote
Alex Just-Alex
(@alex-just-alex)
Active Member
Joined: 3 years ago
Posts: 8
 
import machine, time                       #importing machine and time libraries
from time import sleep                     #importing sleep class
from machine import Pin, ADC, PWM          #importing Pin, ADC and PWM classes
from neopixel import NeoPixel
from ottoneopixel import OttoNeoPixel
from ottobuzzer import OttoBuzzer
from ottomotor import OttoMotor
import utime
led = Pin(2, Pin.OUT)                 # Built in LED
buzzer = OttoBuzzer(25)               # Built in Buzzer
ultrasonic = NeoPixel(Pin(18), 6)     # Connector 1
io = 19                               # echo input and trigger out signal
bright = 0.8                          # brightness variable for lights
n = 13                                # Number of LEDs in ring
ring = OttoNeoPixel(4, n)             # Connector 5
analogL=ADC(Pin(32))                  # Connector 6
analogR=ADC(Pin(33))                  # Connector 7
digitalL = Pin(27, Pin.IN)            # Connector 8
digitalR = Pin(15, Pin.IN)            # Connector 9
motor = OttoMotor(13, 14)             # Connectors 10 & 11
offsetL = 0                           # Calibration for left servo motor
offsetR = 0                           # Calibration for right servo motor
def measure_distance():
   io_pin = Pin(io, Pin.OUT)
   io_pin.off()
   utime.sleep_us(2)
   io_pin.on()
   utime.sleep_us(20)
   io_pin.off()
   io_pin = Pin(io, Pin.IN)
   pulse_duration = machine.time_pulse_us(io_pin, 1)
   distance = 0
   if ((pulse_duration < 60000) and (pulse_duration > 1)):
      distance = pulse_duration / 58.00;
   return distance

while True:
    lsL = analogL.read()
    lsR = analogR.read()
    dist = measure_distance()
    if dist == 0:
        dist = 11 # fudge for lost readings, re reading takes too long
    print("Left sensor value: ", lsL)
    print("Right sensor value: ", lsR)
    print("Dist: ", dist)
    if dist < 10:
        ultrasonic[0] = int(255* bright), int(255 * bright), int(255 * bright)
        ultrasonic[1] = int(255 * bright), int(255 * bright), int(255 * bright)
        ultrasonic[2] = int(255 * bright), int(255 * bright), int(255 * bright)
        ultrasonic[3] = int(254 * bright), int(255 * bright), int(255 * bright)
        ultrasonic[4] = int(254 * bright), int(255 * bright), int(255 * bright)
        ultrasonic[5] = int(254 * bright), int(255 * bright), int(255 * bright)
        ultrasonic.write()
        motor.leftServo.duty(60- offsetL)
        motor.rightServo.duty(90+ offsetR)
        sleep(.5)
        motor.leftServo.duty(90+ offsetL)
        motor.rightServo.duty(90- offsetR)
        sleep(2.5)
    elif lsL > 1200 and lsR < 1200:
        ultrasonic[0] = int(254 * bright), int(0 * bright), int(0 * bright)
        ultrasonic[1] = int(254 * bright), int(0 * bright), int(0 * bright)
        ultrasonic[2] = int(254 * bright), int(0 * bright), int(0 * bright)
        ultrasonic[3] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[4] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[5] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic.write()
        motor.leftServo.duty(68+ offsetL)
        motor.rightServo.duty(68- offsetR)        
        sleep(0.02)
        motor.leftServo.duty(0)
        motor.rightServo.duty(0)    
    elif lsR > 1200 and lsL < 1200:
        ultrasonic[0] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[1] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[2] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[3] = int(254 * bright), int(0 * bright), int(0 * bright)
        ultrasonic[4] = int(254 * bright), int(0 * bright), int(0 * bright)
        ultrasonic[5] = int(254 * bright), int(0 * bright), int(0 * bright)
        ultrasonic.write()
        motor.leftServo.duty(83+ offsetL)
        motor.rightServo.duty(83- offsetR)      
        sleep(0.02)
        motor.leftServo.duty(0)
        motor.rightServo.duty(0)    
    else:
        ultrasonic[0] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[1] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[2] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[3] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[4] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic[5] = int(0 * bright), int(255 * bright), int(0 * bright)
        ultrasonic.write()
        motor.leftServo.duty(85+ offsetL)
        motor.rightServo.duty(65- offsetR)



   
Camilo reacted
ReplyQuote
Alex Just-Alex
(@alex-just-alex)
Active Member
Joined: 3 years ago
Posts: 8
 

I decided it was time that I learned block programming so here is a version in block programming



   
ReplyQuote
Alex Just-Alex
(@alex-just-alex)
Active Member
Joined: 3 years ago
Posts: 8
 

The thing to note is that by setting variables to the line sensor values and the distance at the start of the loop, I don't have to keep remeasuring them until the loop starts again



   
Camilo reacted
ReplyQuote
Camilo
(@cparrapa)
Developer Admin
Joined: 3 years ago
Posts: 112
 

Very good solution @alex-just-alex this we will add as example code and also it is a good lesson to learn about the importance of using variables in this cases. @abacusan you can try, thanks to Alex code this is solved.



   
ReplyQuote
Camilo
(@cparrapa)
Developer Admin
Joined: 3 years ago
Posts: 112
 

Hi @alex-just-alex @ivanr3d

Checked the attached file, I put both block code and text code in one .zip like this we can easily share files in the forum.

Cheers!



   
ReplyQuote
Page 1 / 2
Scroll to Top
This will be available with the release of this product.