Coordinating a Zaber device's movement with sensor readings

407 views Asked by At

I have written a script in Zaber Console to control my stages, but now I want to take some sensor readings during the movement. I want to move the stages, take a reading, and then move the stages again. Sometimes the sensor readings will affect the movements I need to make. How can I coordinate the movement with the sensor readings?

I want to talk to my Zaber devices on one serial port, and my sensor on a second serial port.

1

There are 1 answers

0
Don Kirkby On BEST ANSWER

When you write a Zaber Console script, you have access to everything in the Microsoft.NET framework, so you can open a second serial port yourself. Here's a simple example that tries to maintain a constant pressure on a pressure sensor. Each time through the loop, it reads the pressure sensor, then either retracts the device to increase the pressure or extends the device to decrease the pressure.

// C# example that shows how to communicate with a second serial port
#template(simple)

const string LOAD_PORT = "COM9";
const int MAX_SPEED = 20000;
const int TARGET_PRESSURE = 1000; // in thousandths of a pound

// set up load cell on serial port
using (var port = new System.IO.Ports.SerialPort())
{
    port.PortName = "COM9";
    port.BaudRate = 9600;
    port.ReadTimeout = 500;
    port.WriteTimeout = 500;

    port.Open();

    port.WriteLine("ct0\r");
    port.ReadLine();

    while ( ! IsCanceled)
    {
        port.WriteLine("o0w1\r");
        var pressure = Int32.Parse(port.ReadLine());
        int velocity = (pressure - TARGET_PRESSURE) * 20;
        int speed = Math.Min(Math.Abs(velocity), MAX_SPEED);
        velocity = speed * Math.Sign(velocity);
        Conversation.Device.Send(Command.MoveAtConstantSpeed, velocity);
    }
}

The using block makes sure that you close the serial port even when errors occur. The pressure sensor I used understood ct0 as a tare command and o0w1 as a command to return the current pressure, you'll need to look at your sensor's manual to see what commands it understands. You'll also need to look up what baud rate and other settings to use. The SerialPort documentation lists all the communication parameters you can set.

If you want to record the data from the sensor readings, one option is to just use Output.WriteLine(data) to dump the data values in the output window. Then you can copy and paste them into a spreadsheet or some other file. If you want to write directly to the file, look at the File.CreateText() method.