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
- File → New Project → name it
FirstProject. - Keep the default resolution (1920×1080).
- Modules
Run1andView1appear in Solution Explorer.
Define IO
In Tools → IO Editor add two inputs and one output.
| Name | Type | Description |
|---|---|---|
I_StartBtn | DI | Start button |
I_CnvSens | DI | Conveyor arrival sensor |
O_CnvRun | DO | Conveyor run relay (for test) |
Define the motor
Tools → Motor Editor — add one axis.
| Field | Value |
|---|---|
| Name | M_Cnv |
| Board | (real board or virtual axis) |
| UnitPerPulse | 1.0 |
| JogVel / RunVel | 100 / 300 |
| JogAccel / RunAccel | 0.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 toOnStartClick - 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
- Build → Build All — verify no errors.
- Run (Ctrl+R) to start.
- Press the Start button on View1.
- When
I_CnvSensgoes ON,cycleCountincrements 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)thenStop(). - User feedback with
TOWERLAMP.SetLamp(state). - Cross-module variable binding (
Run1::cycleCount).