Help with coding spatial project

Supporting Visual Studio on Windows
IanBloomfield
Phidgetly
Posts: 24
Joined: Thu Feb 15, 2018 4:49 am
Contact:

Help with coding spatial project

Postby IanBloomfield » Wed Feb 28, 2018 12:56 am

Hi,

I'm hoping someone can help.
I am new to any form of coding, trying to learn from the examples but I am finding the interface very confusing on visual studio.
I can get the example projects working to demonstrate all the capabilities of the spatial phidget.

However I can't work out how to alter the multi-servo program to facilitate multiple spatials.

I think my project should be simple, but I don't have enough know how to do it.

I have two spatials connected via a VINT HUB with USB
all I need is to be able to log the accelerometer data and gyroscope data from each spatial and output it to an excel or csv file with a timestamp.

each spatial needs to record simultaneously as I plan to record the movement of a persons hand using these.

Is anyone able to share any code or point out the relevant examples I should be looking at?

the spatial examples don't seem to translate to multiple phidgets and the spatial example of datalogging when I try to run the code it gives 76 errors.

I'm continuing to learn my basic coding but I think I'm a long way off being able to figure this out at present.

IanBloomfield
Phidgetly
Posts: 24
Joined: Thu Feb 15, 2018 4:49 am
Contact:

Re: Help with coding spatial project

Postby IanBloomfield » Wed Feb 28, 2018 10:48 pm

namespace WindowsFormsApp7
{
public partial class Form1 : Form
{
Spatial spatial;

public Form1()
{
InitializeComponent();
}

private void Form1_load(object sender, EventArgs e)
{

spatial = new Spatial();

spatial.Attach += Spatial_Attach;
spatial.Detach += Spatial_Detach;

spatial.DeviceSerialNumber = 496567;

spatial.HubPort = 5;

spatial.Open();



}
private void Spatial_Attach(object sender, Phidget22.Events.AttachEventArgs e)
{
throw new NotImplementedException();
}

private void Spatial_Detach(object sender, Phidget22.Events.DetachEventArgs e)
{
throw new NotImplementedException();
}
}
}

I took the coding from the youtube clip on outputting the thermocouple to a form in visual studio

I have created a form that has 2 labels - Spatial, label2

I took the coding from the screen example for the thermocouple it runs without errors but I can't get any output results to appear in my form?

Is it something else I need to put into the form programming or do I need to modify the form design? I can't find any clear instructions about this?

If I can get this working then I plan to try to modify it to demonstrate the output from both of my phidgets. I have 2 Phidget spatials connected via VINT through ports 0 and 5.


is anyone able to help me get the form example to work?

User avatar
mparadis
Site Admin
Posts: 531
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Help with coding spatial project

Postby mparadis » Thu Mar 01, 2018 8:56 am

For each new channel that you open, you need to provide a new handle (like "spatial" in the code you posted). You create both handles, set different hub ports for each one, and then assign a data handler for each one. In this case, you want the SpatialData event, which triggers each time new data comes from the spatial. You can see how this works by looking at the spatial_SpatialData function in the sample code (make sure you are looking at the Spatial example, not the compass calibrator example).

IanBloomfield
Phidgetly
Posts: 24
Joined: Thu Feb 15, 2018 4:49 am
Contact:

Re: Help with coding spatial project

Postby IanBloomfield » Thu Mar 01, 2018 3:19 pm

Thank you.

You've been very patient with someone clearly out of their depth. I think what I struggled with understanding is how the visual studio breaks up the program. I have the spatial example but I can't work out where is the specific code that I'm looking for. The example utils is too complex and is generic for all so I assume that just created the dials on the form. Which part of the program or what file specifically do I need to be looking at to find the spatial coding?

User avatar
mparadis
Site Admin
Posts: 531
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Help with coding spatial project

Postby mparadis » Fri Mar 02, 2018 9:48 am

You're correct that a lot of the code in the C# examples serves to manipulate the graphical interface and be a general example for any Phidget Spatial device. This is the downside of examples with graphical interfaces, I suppose.

I'll point out the important parts:

In Form1_Load, the spatial channel is defined and opened:

Code: Select all

spatial = new Spatial();
spatial.Attach += spatial_Attach;
spatial.Detach += spatial_Detach;
spatial.Error += spatial_Error;
spatial.SpatialData += spatial_SpatialData;
open.openCmdLine(spatial);


In the above code, the spatial object is defined, and four event handlers are added to it: One that triggers when the channel of the desired type (spatial) attaches, one that triggers when it detaches, one that triggers when a Phidget error occurs, and one that triggers whenever data comes in. Then we attempt to open the spatial. You can duplicate this code for another spatial (use a new handle like spatial2) but set spatial.HubPort to different values for each depending on which hub port they're connected to.

The next code we're interested in is in the function spatial_SpatialData, because this is the function that triggers when data comes in.

Code: Select all

void spatial_SpatialData(object sender, SpatialSpatialDataEventArgs e) {
         
accelXTxt.Text = e.Acceleration[0].ToString() + " g";
accelYTxt.Text = e.Acceleration[1].ToString() + " g";
accelZTxt.Text = e.Acceleration[2].ToString() + " g";
gyroXTxt.Text = e.AngularRate[0].ToString() + " °/s";
gyroYTxt.Text = e.AngularRate[1].ToString() + " °/s";
gyroZTxt.Text = e.AngularRate[2].ToString() + " °/s";
compassXTxt.Text = e.MagneticField[0].ToString() + " G";
compassYTxt.Text = e.MagneticField[1].ToString() + " G";
compassZTxt.Text = e.MagneticField[2].ToString() + " G";
}


This is the relevant part of the data handler- it just updates all of the labels on the interface to the most recent value for each type of data coming in. If you were using multiple spatials, you could use "sender" to figure out which one triggered the event:

Code: Select all

Spatial channel = (Spatial)sender;
int hubPort = channel.HubPort;


Return to “C#.NET”

Who is online

Users browsing this forum: No registered users and 1 guest