Tutorial · Chapter 2

title: "First project — conveyor + sensor + sequence" chapter: 2 source: "Doc/QMachineStudio UserManaul.pdf"

First project — conveyor + sensor

Build a minimal project from scratch: **one conveyor axis + one arrival sensor

  • a simple pick-and-place sequence**.

By the end you'll see how motor, IO, and sequence pieces fit together in XScript.

Create the project

  1. File → New Project → name it FirstProject.
  2. Keep the default resolution (1920×1080).
  3. Modules Run1 and View1 appear in Solution Explorer.

Define IO

In Tools → IO Editor add two inputs and one output.

NameTypeDescription
I_StartBtnDIStart button
I_CnvSensDIConveyor arrival sensor
O_CnvRunDOConveyor run relay (for test)

Define the motor

Tools → Motor Editor — add one axis.

FieldValue
NameM_Cnv
Board(real board or virtual axis)
UnitPerPulse1.0
JogVel / RunVel100 / 300
JogAccel / RunAccel0.2 / 0.3

Write the sequence

Paste into Run1.xms. Opening braces always on the next line.

int cycleCount = 0;
 
FUNCTION OnSystemStart()
{
    Log("Start");
    TOWERLAMP.SetLamp("RUN");
    return true;
}
 
FUNCTION OnSystemStop()
{
    MOTOR["M_Cnv"].Stop();
    IO["O_CnvRun"] = OFF;
    TOWERLAMP.SetLamp("STOP");
    return true;
}
 
////SEQUENCE,Seq
 
////STEP,STEP_Idle
Sleep(100);
 
if (IO["I_StartBtn"] == OFF)
{
    return false;
}
 
Log($"Cycle {cycleCount} begin");
 
////STEP,STEP_Run
IO["O_CnvRun"] = ON;
MOTOR["M_Cnv"].SetSpeed("RUN");
if (MOTOR["M_Cnv"].MoveVel(true) == false)
{
    MOTOR["M_Cnv"].ShowMotorError(ModuleName);
    goto "STEP_Error";
}
 
////STEP,STEP_WaitSensor
if (IO["I_CnvSens"].CheckContiOn(500) == false)
{
    return false;
}
 
MOTOR["M_Cnv"].Stop();
IO["O_CnvRun"] = OFF;
 
cycleCount++;
Log($"Cycle {cycleCount} done");
 
goto "STEP_Idle";
 
////STEP,STEP_Error
TOWERLAMP.SetLamp("ERROR");
Sleep(1000);
goto "STEP_Idle";

Lay out the view

Open View1.xdf and drop one button and one label.

  • Button btnStart → bind Click event to OnStartClick
  • TextBlock txtCount{Binding Run1::cycleCount}
// View1.xms
FUNCTION OnStartClick(string sender, int tag, array params)
{
    IO["I_StartBtn"].WriteBit(ON);
    Sleep(200);
    IO["I_StartBtn"].WriteBit(OFF);
}

Run it

  1. Build → Build All — verify no errors.
  2. Run (Ctrl+R) to start.
  3. Press the Start button on View1.
  4. When I_CnvSens goes ON, cycleCount increments and the sequence returns to idle.

What you learned

  • Breaking a sequence into steps with ////STEP,STEP_name.
  • State transitions via goto "STEP_name".
  • Noise-safe waiting with IO["name"].CheckContiOn(ms).
  • Velocity mode + stop pattern: MoveVel(true) then Stop().
  • User feedback with TOWERLAMP.SetLamp(state).
  • Cross-module variable binding (Run1::cycleCount).

Next steps