Skip to content

Commit

Permalink
Sixth commit
Browse files Browse the repository at this point in the history
  • Loading branch information
visose committed Nov 8, 2015
1 parent 2c9d840 commit e4f2944
Show file tree
Hide file tree
Showing 14 changed files with 646 additions and 150 deletions.
18 changes: 9 additions & 9 deletions Robots/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ public string Code(Robot robot, Target target)

public class Wait : ICommand
{
double seconds = 0;
internal double Seconds { get; }

public Wait(double seconds)
{
this.seconds = seconds;
this.Seconds = seconds;
}

public string Code(Robot robot, Target target)
Expand All @@ -78,33 +78,33 @@ public string Code(Robot robot, Target target)
case (Robot.Manufacturers.ABB):
{
if (target.Zone.IsFlyBy)
return $@"WaitTime {seconds:0.000}";
return $@"WaitTime {Seconds:0.000};";
else
return $@"WaitTime \InPos,{seconds:0.000}";
return $@"WaitTime \InPos,{Seconds:0.000};";
}

case (Robot.Manufacturers.KUKA):
{
if (target.Zone.IsFlyBy)
return $"CONTINUE\r\nWAIT SEC {seconds:0.000}";
return $"CONTINUE\r\nWAIT SEC {Seconds:0.000}";
else
return $"WAIT SEC {seconds:0.000}";
return $"WAIT SEC {Seconds:0.000}";
}

case (Robot.Manufacturers.UR):
{
if (target.Zone.IsFlyBy)
return $"sleep({seconds:0.000})";
return $"sleep({Seconds:0.000})";
else
return $"sleep({seconds:0.000})";
return $"sleep({Seconds:0.000})";
}

default:
return null;
}
}

public override string ToString() => $"Command (Wait {seconds} secs)";
public override string ToString() => $"Command (Wait {Seconds} secs)";
}

public class SetDO : ICommand
Expand Down
233 changes: 232 additions & 1 deletion Robots/Grasshopper/Kinematics.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using Grasshopper.Kernel;
using Grasshopper.Kernel.Types;
using Grasshopper.GUI;
using Grasshopper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Drawing;

namespace Robots.Grasshopper
{
Expand Down Expand Up @@ -35,7 +39,7 @@ protected override void SolveInstance(IGH_DataAccess DA)
if (!DA.GetData(0, ref robot)) { return; }
if (!DA.GetData(1, ref target)) { return; }

var kinematics = robot.Value.Kinematics(target.Value);
var kinematics = robot.Value.Kinematics(target.Value, true);

if (kinematics.Errors.Count > 0)
{
Expand Down Expand Up @@ -82,4 +86,231 @@ protected override void SolveInstance(IGH_DataAccess DA)
DA.SetData(0, radiansText);
}
}

public class Simulation : GH_Component
{
public Simulation() : base("Program simulation", "Sim", "Simulation of the robot program", "Robots", "Components")
{
form = new AnimForm(this);
}
public override GH_Exposure Exposure => GH_Exposure.quarternary;
public override Guid ComponentGuid => new Guid("{6CE35140-A625-4686-B8B3-B734D9A36CFC}");
protected override System.Drawing.Bitmap Icon => null;// Properties.Resources.iconCheckProgram;

protected override void RegisterInputParams(GH_Component.GH_InputParamManager pManager)
{
pManager.AddParameter(new ProgramParameter(), "Program", "P", "Program", GH_ParamAccess.item);
pManager.AddNumberParameter("Time", "T", "Advance the simulation to this time", GH_ParamAccess.item, 0);
pManager.AddBooleanParameter("Normalized", "N", "Time value is normalized (from 0 to 1)", GH_ParamAccess.item, true);
}

protected override void RegisterOutputParams(GH_Component.GH_OutputParamManager pManager)
{
pManager.AddMeshParameter("Robot meshes", "M", "Robot meshes", GH_ParamAccess.list);
pManager.AddNumberParameter("Joint rotations", "J", "Joint rotations", GH_ParamAccess.list);
pManager.AddPlaneParameter("Planes", "P", "Planes", GH_ParamAccess.list);
pManager.AddTextParameter("Errors", "E", "Errors", GH_ParamAccess.list);
}

protected override void SolveInstance(IGH_DataAccess DA)
{
GH_Program program = null;
GH_Number time = null;
GH_Boolean isNormalized = null;
if (!DA.GetData(0, ref program)) { return; }
if (!DA.GetData(1, ref time)) { return; }
if (!DA.GetData(2, ref isNormalized)) { return; }

var kinematics = program.Value.Animate((form.Visible) ? formTime : time.Value, (form.Visible) ? false : isNormalized.Value);

if (kinematics.Errors.Count > 0)
{
this.AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Errors in solution");
}

DA.SetDataList(0, kinematics.Meshes.Select(x => new GH_Mesh(x)));
DA.SetDataList(1, kinematics.JointRotations);
DA.SetDataList(2, kinematics.Planes);
DA.SetDataList(3, kinematics.Errors);

if (form.Visible && form.play.Checked)
{
var currentTime = DateTime.Now;
TimeSpan delta = currentTime - lastTime;
formTime += delta.TotalSeconds * speed;
lastTime = currentTime;
this.ExpireSolution(true);
}
}

// Form
AnimForm form;
double formTime = 0;
double speed = 1;
DateTime lastTime;

protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
{
Menu_AppendItem(menu, "Open controls", OpenForm, true, form.Visible);
}

void OpenForm(object sender, EventArgs e)
{
if (form.Visible)
form.Hide();
else
{
form.Show(Instances.DocumentEditor);
GH_WindowsFormUtil.CenterFormOnCursor(form, true);
}
}

void ClickPlay(object sender, EventArgs e)
{
lastTime = DateTime.Now;
ExpireSolution(true);
}

void ClickStop(object sender, EventArgs e)
{
form.play.Checked = false;
formTime = 0.0;
ExpireSolution(true);
}

void ClickScroll(object sender, EventArgs e)
{
double trackValue = form.slider.Value;

if (form.slider.Value % form.slider.SmallChange != 0)
{
form.slider.Value = (form.slider.Value / form.slider.SmallChange) * form.slider.SmallChange;
}

speed = (double)form.slider.Value / 100.0;
}

partial class AnimForm : Form
{
Simulation component;

internal CheckBox play = new CheckBox();
private Button stop = new Button();
internal TrackBar slider = new TrackBar();

public AnimForm(Simulation component)
{
this.component = component;
InitializeComponent();
}

protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.UserClosing)
{
e.Cancel = true;
this.Hide();
}
}

private System.ComponentModel.IContainer components = null;

protected override void Dispose(bool disposing)
{
if (disposing && (components != null)) components.Dispose();
base.Dispose(disposing);
}

private void InitializeComponent()
{
this.SuspendLayout();

var controlFont = new Font(FontFamily.GenericSansSerif, 20f, FontStyle.Regular, GraphicsUnit.Pixel);
var labelFont = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Regular, GraphicsUnit.Pixel);

// Form
Controls.Add(play);
Controls.Add(stop);

// AutoScaleDimensions = new SizeF(6F, 13F);
AutoScaleMode = AutoScaleMode.None;
AutoSize = false;
MinimumSize = new Size(0, 0);
Size = new Size(138, 320);
Name = "Simulation controls";
Text = "Simulation";
ResumeLayout(false);
FormBorderStyle = FormBorderStyle.FixedSingle;
MinimizeBox = false;
MaximizeBox = false;
ShowIcon = false;
TransparencyKey = Color.White;

// Play
play.Location = new Point(6, 6);
play.Size = new Size(60, 60);
play.Name = "Play";
play.Text = "\u25B6";
play.Font = controlFont;
play.TextAlign = ContentAlignment.MiddleCenter;
play.TabIndex = 0;
play.UseVisualStyleBackColor = true;
play.Click += new EventHandler(component.ClickPlay);
play.Appearance = Appearance.Button;

// Stop
stop.Location = new Point(66, 6);
stop.Size = new Size(60, 60);
stop.Name = "Pause";
stop.Text = "\u25FC";
play.Font = controlFont;
play.TextAlign = ContentAlignment.MiddleCenter;
stop.TabIndex = 1;
stop.UseVisualStyleBackColor = true;
stop.Click += new EventHandler(component.ClickStop);


// Slider group
var group = new GroupBox();
group.Location = new Point(6, 66);
group.Size = new Size(120, 220);
group.Text = "Speed";
group.Font = labelFont;
this.Controls.Add(group);

// Slider
slider.Location = new Point(6, 14);
slider.Size = new Size(45, 200);
slider.Orientation = Orientation.Vertical;
slider.Name = "Speed";
slider.TabIndex = 2;
slider.Maximum = 400;
slider.Minimum = -200;
slider.TickFrequency = 100;
slider.LargeChange = 100;
slider.SmallChange = 50;
slider.TickStyle = TickStyle.BottomRight;
slider.Value = 100;
slider.ValueChanged += new EventHandler(component.ClickScroll);
group.Controls.Add(slider);

// Slider labels
int count = (slider.Maximum - slider.Minimum)/ slider.TickFrequency;

for(int i=0;i<=count;i++)
{
var label = new Label();

label.Location = new Point(51, 16+i*29);
label.Size = new Size(60, 20);
label.Text = (slider.Maximum - i * 100).ToString() + "%";
label.Font = labelFont;
label.TextAlign = ContentAlignment.MiddleLeft;
group.Controls.Add(label);
}
}
}
}

}
2 changes: 1 addition & 1 deletion Robots/Grasshopper/Parameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected override GH_GetterResult Prompt_Plural(ref List<GH_Program> values)
{
values = new List<GH_Program>();
return GH_GetterResult.success;
}
}
}

public class TargetParameter : GH_PersistentParam<GH_Target>
Expand Down

0 comments on commit e4f2944

Please sign in to comment.