API Guides > ConceptRT 3.x
Machine/main.cpp

todo link to download the zip project.

Machine.h

#ifndef _MACHINE_H_
#define _MACHINE_H_
#include "ConceptRTModel.h"
using namespace ConceptRT;
namespace Example
{
class MachineElement : public ConceptComponent
{
};
class Machine : public ConceptComponent
{
CONCEPT_BEGIN(Machine, "Example.Machine", ConceptComponent)
CONCEPT_OBJECT(MachineElements)
};
}
#endif

Gripper.h

#ifndef _GRIPPER_H_
#define _GRIPPER_H_
#include "Machine.h"
namespace Example
{
class Gripper : public MachineElement
{
PROPERTY_GET_SET(TimeSpan, OpeningTimeout)
CONCEPT_BEGIN(Gripper, "Example.Gripper", MachineElement)
CONCEPT_ITEM(OpeningTimeout)
public:
void Open() { System::GetConsole().WriteLine(GetName() + " - Open()"); }
void Close(){ System::GetConsole().WriteLine(GetName() + " - Close()");}
};
}
#endif

RobotArm.h

#ifndef _ROBOTARM_H_
#define _ROBOTARM_H_
#include "Machine.h"
namespace Example
{
struct Point : public DataStructure
{
};
class RobotArm : public MachineElement
{
PROPERTY_GET_SET(Float32, MaxPositionX)
PROPERTY_GET_SET(Float32, MaxPositionY)
PROPERTY_GET_SET(Float32, MaxPositionZ)
CONCEPT_BEGIN(RobotArm, "Example.RobotArm", MachineElement)
CONCEPT_ITEM(MaxPositionX)
CONCEPT_ITEM(MaxPositionY)
CONCEPT_ITEM(MaxPositionZ)
public:
void MoveTo(Point point)
{
System::GetConsole().WriteLine(GetName()
+ " - MoveTo(x : " + ToString(point.x)
+ " y : " + ToString(point.y) +")");
}
};
}
#endif

Process.h

#ifndef _PROCESS_H_
#define _PROCESS_H_
#include "ConceptRTOs.h"
#include "ConceptRTModel.h"
using namespace ConceptRT;
namespace Example
{
class Action : public ConceptComponent
{
public:
virtual void Execute()
{
System::GetConsole().WriteLine("==== > Execute action : " + GetName());
}
};
class Process : public ConceptComponent
{
CONCEPT_BEGIN(Process, "Example.Process", ConceptComponent)
CONCEPT_OBJECT(Actions)
public:
void Execute()
{
Int32 count = GetActions().GetCount();
for (Int32 index = 0; index < count; index++)
{
GetActions()[index].Execute();
}
}
};
}
#endif

ActionGripper.h

#ifndef _ACTION_GRIPPER_H_
#define _ACTION_GRIPPER_H_
#include "Process.h"
#include "Gripper.h"
namespace Example
{
class ActionOpen : public Action
{
PROPERTY_OBJECT(ConceptSingleLink<Gripper>, Gripper) // aggregation
CONCEPT_BEGIN(ActionOpen, "Example.ActionOpen", Action)
CONCEPT_OBJECT(Gripper)
public:
void Execute() override
{
baseClass::Execute();
if (GetGripper().IsValidReference())
{
GetGripper().GetReference()->Open();
}
}
};
}
#endif

ActionMove.h

#ifndef _ACTION_MOVE_H_
#define _ACTION_MOVE_H_
#include "Process.h"
#include "RobotArm.h"
namespace Example
{
class ActionMove : public Action
{
PROPERTY_OBJECT(ConceptSingleLink<RobotArm>, Arm) // aggregation
PROPERTY_OBJECT(Point, Point)
CONCEPT_BEGIN(ActionMove, "Example.ActionMove", Action)
public:
void Execute() override
{
baseClass::Execute();
if (GetArm().IsValidReference())
{
GetArm().GetReference()->MoveTo(GetPoint());
}
}
};
}
#endif

ActionPickPlace.h

#ifndef _ACTION_PICKPLACE_H_
#define _ACTION_PICKPLACE_H_
#include "Process.h"
#include "Gripper.h"
#include "RobotArm.h"
namespace Example
{
class ActionPickPlace : public Action
{
PROPERTY_OBJECT(ConceptSingleLink<Gripper>, Gripper) // aggregation
PROPERTY_OBJECT(ConceptSingleLink<RobotArm>, Arm) // aggregation
PROPERTY_OBJECT(Point, PointPick)
PROPERTY_OBJECT(Point, PointPlace)
CONCEPT_BEGIN(ActionPickPlace, "Example.ActionPickPlace", Action)
CONCEPT_OBJECT(Gripper)
CONCEPT_OBJECT(PointPick)
CONCEPT_OBJECT(PointPlace)
public:
void Execute() override
{
baseClass::Execute();
if (!GetGripper().IsValidReference() || !GetArm().IsValidReference())
{
System::GetConsole().WriteLine("Bad dependencies");
return;
}
// pick
GetGripper().GetReference()->Open();
GetArm().GetReference()->MoveTo(GetPointPick());
GetGripper().GetReference()->Close();
// place
GetArm().GetReference()->MoveTo(GetPointPlace());
GetGripper().GetReference()->Open();
}
};
}
#endif

main.cpp

#include "ConceptRTOs.h"
using namespace ConceptRT;
#include "Machine.h"
#include "Gripper.h"
#include "RobotArm.h"
#include "Process.h"
#include "ActionGripper.h"
#include "ActionMove.h"
#include "ActionPickPlace.h"
using namespace Example;
void LoadMachine(Machine & machine)
{
String filePath = System::GetApplicationPath() + "machine.xml";
if (!FileExists(filePath))
{
System::GetConsole().WriteLine("Machine configuration hardcoded.");
// Machine configuration (hard coded) : Populate the machine elements list
Gripper & gripperA = machine.GetMachineElements().AddNew<Gripper>("GripperA");
gripperA.SetOpeningTimeout(Seconds(2)); // sets the properties
RobotArm & armA = machine.GetMachineElements().AddNew<RobotArm>("ArmA");
armA.SetMaxPositionX(45.6f); // sets the properties
armA.SetMaxPositionY(87.2f); // sets the properties
armA.SetMaxPositionZ(28.1f); // sets the properties
Gripper & gripperB = machine.GetMachineElements().AddNew<Gripper>("GripperB");
gripperB.SetOpeningTimeout(Milliseconds(250)); // sets the properties
RobotArm & armB = machine.GetMachineElements().AddNew<RobotArm>("ArmB");
armB.SetMaxPositionX(34.7f); // sets the properties
armB.SetMaxPositionY(88.4f); // sets the properties
armB.SetMaxPositionZ(62.3f); // sets the properties
// Machine configuration save to filesystem
FileStream machineOutfile(filePath, FileModeTextWrite);
XmlDataStoreWriter writer(machineOutfile);
writer.WriteValue(machine.GetName(), machine);
machineOutfile.Close();
}
else
{
System::GetConsole().WriteLine("Machine configuration loaded from file " + filePath);
// Clearing machine
machine.Clear();
// Machine configuration loaded from filesystem
// Build factories list
ConceptFactoriesDynamic machineFactories;
machineFactories.RegisterFactory(Gripper::GetClassFactory());
machineFactories.RegisterFactory(RobotArm::GetClassFactory());
// Open the file
FileStream machineInfile(filePath, FileModeTextRead);
XmlDataStoreReader xmlReader(machineInfile);
// Create and configure dataStore for polymorphous model deserialization
ConceptDataStore reader(xmlReader);
reader.SetFactories(machineFactories);
// Read the machine
reader.ReadValue(machine.GetName(), machine);
machine.ResolveLinks();
machineInfile.Close();
}
}
void LoadProcess(Process & process, Machine & machine)
{
String filePath = System::GetApplicationPath() + "process.xml";
if (!FileExists(filePath))
{
System::GetConsole().WriteLine("Process configuration hardcoded.");
// Get a specific machine element
RobotArm * arm = machine.GetMachineElements().FindByName<RobotArm>("ArmA");
if (arm != NULL)
System::GetConsole().WriteLine("Restored armA Max position x : " + ToString(arm->GetMaxPositionX()));
else
System::GetConsole().WriteLine("ArmA not found. Item not present or bad type.");
Gripper * gripper = machine.GetMachineElements().FindByName<Gripper>("GripperA");
if (gripper == NULL)
System::GetConsole().WriteLine("GripperA not found. Item not present or bad type.");
// process configuration (hard coded)
Gripper & gripperUsed = *gripper;
RobotArm & armUsed = *arm;
ActionOpen & action1 = process.GetActions().AddNew<ActionOpen>("Action1");
// action dependencies
action1.GetGripper().SetReference(gripperUsed);
ActionPickPlace & action2 = process.GetActions().AddNew<ActionPickPlace>("Action2");
// action dependencies
action2.GetGripper().SetReference(gripperUsed);
action2.GetArm().SetReference(armUsed);
// Point pick
action2.GetPointPick().x = 10;
action2.GetPointPick().y = 20;
action2.GetPointPick().z = 15;
// Point place
action2.GetPointPlace().x = 30;
action2.GetPointPlace().y = -20;
action2.GetPointPlace().z = 0;
ActionMove & action3 = process.GetActions().AddNew<ActionMove>("Action3");
// action dependencies
action3.GetArm().SetReference(armUsed);
// Point pick
action3.GetPoint().x = 0;
action3.GetPoint().y = 0;
action3.GetPoint().z = 0;
// Process configuration save to filesystem
FileStream processOutfile(filePath, FileModeTextWrite);
XmlDataStoreWriter writer(processOutfile);
writer.WriteValue(process.GetName(), process);
processOutfile.Close();
}
else
{
System::GetConsole().WriteLine("Process configuration loaded from file " + filePath);
// Clearing process
process.Clear();
// Process configuration loaded from filesystem
// Build factories list
ConceptFactoriesDynamic processFactories;
processFactories.RegisterFactory(ActionOpen::GetClassFactory());
processFactories.RegisterFactory(ActionPickPlace::GetClassFactory());
processFactories.RegisterFactory(ActionMove::GetClassFactory());
// Open the file
FileStream processInfile(filePath, FileModeTextRead);
XmlDataStoreReader xmlReader(processInfile);
// Create and configure dataStore for polymorphous model deserialization
ConceptDataStore reader(xmlReader);
reader.SetFactories(processFactories);
// Read the machine
reader.ReadValue(process.GetName(), process);
process.ResolveLinks();
processInfile.Close();
}
}
{
// Machine declaration
Machine machine;
machine.SetName("Machine");
LoadMachine(machine);
// Process declaration
Process process;
process.SetName("Process");
LoadProcess(process, machine);
// Execution of the process
System::GetConsole().WriteLine("Process execution");
System::GetConsole().WriteLine("-----------------");
process.Execute();
System::GetConsole().WriteLine("Process execution end.");
}
{
// without licence
}