Asked — Edited

Sdk Scan Speed Of HC-SR04

Is there a way of adusting the speed of the scan of the HC-SR04 using the SDK or do you have to do your own timing ? I noticed in the ARC the interval changes the speed of the sweep, but i can't seem to find any interval in the SDK anywhere for interval, am I missing it somewhere ?

Thanks Mike


ARC Pro

Upgrade to ARC Pro

Stay at the forefront of robot programming innovation with ARC Pro, ensuring your robot is always equipped with the latest advancements.

PRO
Synthiam
#1  

Would you be able to post your code as an example for me to get an idea of where you're at?

#2  

Here is the part of the code i'm working on.

    private void getDistances()
    {
	//Start on the right side
          ezb.EZB.Servo.SetServoPosition(_SCAN_SERVO_PIN, _SCAN_RIGHT_MIN);   

        for (int x = _SCAN_RIGHT_MIN; x <= _SCAN_LEFT_MAX; x += 2)
        {

            ezb.EZB.HC_SR04.MinPoolTimeMS = 1;		//Doesn't seem to see all objects at times above 3
	
	//Set the servo position to x
            ezb.EZB.Servo.SetServoPosition(_SCAN_SERVO_PIN, x);

	//Get the distance of the object  
            int distance = ezb.EZB.HC_SR04.GetDistance(_PING_TRIGGER_PIN, _PING_ECHO_PIN);

	//Save the distance in an array indexed by the servo position
            _markDirArray[ezb.EZB.Servo.GetServoPosition(_SCAN_SERVO_PIN)] = distance;  

	//Show distance and direction on screen
            log("Distance: {0}, Dir: {1}", distance, ezb.EZB.Servo.GetServoPosition(_SCAN_SERVO_PIN));  
        }
        
    }

    private void findShortestPath()
    {


        log("Started");

    //Set _minDist to a large number
        _minDist = 100;
        zz = false;
        do
        {
            // Find all objects
            getDistances();

            // Get the shortest path

// getShortestDist();

// turnTowardShortest();

             Application.DoEvents();
            System.Threading.Thread.Sleep(1150);
        } while (zz == false);
    }


   private void timer1_Tick(object sender, EventArgs e)
    {
        if (_inProgress)
            return;
        if (!ezb.EZB.IsConnected)
        {
            return;
        }

        _inProgress = true;

        findShortestPath();



        Application.DoEvents();

        _inProgress = false;

    }


}
PRO
Synthiam
#3  

Here, try this. I use the timer to control the speed of the sweep. A timer is a good idea because it also allows the UI to respond. If you put the sweep in a FOR loop, then you lock up the UI thread.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tutorial_20___Find_Shortest_Path {

  public partial class Form1 : Form {

    // Used in timer to avoid double entry
    bool         _inProgress     = false; 

    // servo port of scanner
    readonly EZ_B.Servo.ServoPortEnum _SCAN_SERVO_PIN = EZ_B.Servo.ServoPortEnum.RB5;

    // HC-SR04 Sensor Pins
    readonly EZ_B.Digital.DigitalPortEnum _PING_TRIGGER_PIN = EZ_B.Digital.DigitalPortEnum.RC0;
    readonly EZ_B.Digital.DigitalPortEnum _PING_ECHO_PIN = EZ_B.Digital.DigitalPortEnum.RC1;

    // False scans right, True scans left
    bool         _scanDir        = false;

    // Array of distance values
    int [] _markDirArray = new int[EZ_B.Servo.SERVO_MAX + 1];

    public Form1() {

      InitializeComponent();

      timer1.Interval = 200;
      timer1.Start();
    }

    delegate void SetLogCallback(string text, params object[] vals);
    private void Log(string text, params object[] vals) {

      if (this.tbLog.InvokeRequired) {

        SetLogCallback d = new SetLogCallback(Log);
        this.Invoke(d, new object[] { text, vals });
      } else {

        this.tbLog.AppendText(string.Format(text, vals));
        this.tbLog.AppendText(Environment.NewLine);
      }
    }

    private void doSweepServo() {

      int currentPosition = ezb.EZB.Servo.GetServoPosition(_SCAN_SERVO_PIN);

      _markDirArray[currentPosition] = ezb.EZB.HC_SR04.GetDistance(_PING_TRIGGER_PIN, _PING_ECHO_PIN);

      Log("Position: {0}, Distance: {1}", currentPosition, _markDirArray[currentPosition]);

      if (_scanDir)
        currentPosition++;
      else
        currentPosition--;

      ezb.EZB.Servo.SetServoPosition(_SCAN_SERVO_PIN, currentPosition);

      if (currentPosition == EZ_B.Servo.SERVO_MAX)
        _scanDir = false;
      else if (currentPosition == EZ_B.Servo.SERVO_MIN)
        _scanDir = true;
    }

    private void timer1_Tick(object sender, EventArgs e) {

      if (!ezb.EZB.IsConnected)
        return;

      if (_inProgress)
        return;

      _inProgress = true;

      doSweepServo();

      _inProgress = false;
    }
  }
}

#4  

Thanks

Got it working now, i guess i was just brain dead for a little while.

Thanks alot Mike

PRO
Synthiam
#5  

Hehehe, glad it worked out Mike! :D