//called when data for any output pin is requested public void Evaluate(int SpreadMax) { if (this.FInReopen[0]) { if (this.FBusGroup != null) { this.FBusGroup.Close(); this.FBusGroup = null; } } if (this.FBusGroup == null) { GC.Collect(); this.FBusGroup = new BusGroup(); this.FBusGroup.Open(500000); this.FBusGroup.Refresh(); } if (FInRefresh[0]) { this.FBusGroup.Refresh(); } this.FBusGroup.Update(); var motors = this.FBusGroup.GetAllMotors(); this.FOutMotorID.AssignFrom(motors.Keys); this.FOutMotors.AssignFrom(motors.Values); this.FOutBusGroup[0] = this.FBusGroup; }
public void Dispose() { if (this.FBusGroup != null) { this.FBusGroup.Close(); this.FBusGroup = null; } }
static void SendToAllPrimary(BusGroup busGroup, int value, bool blocking) { var motors = busGroup.GetAllMotors(); foreach (var motor in motors.Values) { motor.SetPrimaryRegister(value, blocking); } Finalise(busGroup); }
static void SendToAll(BusGroup busGroup, Messages.RegisterType registerType, int value, bool blocking) { var motors = busGroup.GetAllMotors(); foreach (var motor in motors.Values) { motor.SetRegister(registerType, value, blocking); } Finalise(busGroup); }
static void Finalise(BusGroup busGroup) { busGroup.BlockUntilActionsComplete(new TimeSpan(0, 0, 10)); busGroup.Update(); var errors = busGroup.GetAllErrors(); foreach (var error in errors) { Console.WriteLine(error); } }
//called when data for any output pin is requested public void Evaluate(int SpreadMax) { if (!FAlternativeDevicesRegistered) { GCAN.Initializer.RegisterDevices(); FAlternativeDevicesRegistered = true; } if (this.FBusGroup == null) { this.FBusGroup = new BusGroup(); } if (FInRestart[0] || FInBitrate.IsChanged) { this.FBusGroup.Close(); } if (!FBusGroup.IsOpen && FInEnabled[0]) { this.FBusGroup.Open(this.FInBitrate[0]); this.FBusGroup.Refresh(); } else if (FBusGroup.IsOpen && !FInEnabled[0]) { this.FBusGroup.Close(); } if (FInRefresh[0] && this.FBusGroup.IsOpen) { this.FBusGroup.Refresh(); } this.FOutBusgroup[0] = this.FBusGroup; this.FOutIsOpen[0] = this.FBusGroup.IsOpen; if (FBusGroup.IsOpen) { // This must be called regularly (e.g. once per mainloop frame) this.FBusGroup.Update(); this.FOutBuses.AssignFrom(this.FBusGroup.Buses); var motors = this.FBusGroup.GetAllMotors(); this.FOutMotorID.AssignFrom(motors.Keys); this.FOutMotors.AssignFrom(motors.Values); } else { this.FOutBuses.SliceCount = 0; this.FOutMotors.SliceCount = 0; this.FOutMotorID.SliceCount = 0; } }
static void ForceSendToAllPrimary(BusGroup busGroup, int value, bool blocking) { var writeRequest = new Messages.WritePrimaryRegisterRequest(1, value); for (int i = 1; i <= maxMotorID; i++) { writeRequest.ID = i; Parallel.ForEach(busGroup.Buses, (bus) => { bus.Send(writeRequest, blocking); Program.txCountTotal++; }); } }
static void Main(string[] args) { var busGroup = new BusGroup(); Console.WriteLine("Opening bus..."); busGroup.Open(500000); // Refresh the motors (for debugging) busGroup.Refresh(); { var motors = busGroup.GetAllMotors(); Console.WriteLine("Found {0} motors", motors.Count); if (motors.Count == 0) { busGroup.Close(); return; } } // Enable Torque ForceSendToAll(busGroup, Messages.RegisterType.ControlMode, 1, true); // Disable screen (movements are smoother) ForceSendToAll(busGroup, Messages.RegisterType.InterfaceEnabled, 0, true); // Perform movement N times int N = 1000; for (int n = 0; n < N; n++) { Console.WriteLine("Iteration {0}/{1}...", n, N); int i = 0; int limit = 1 << 10; int amp = 13; for (; i < limit; i++) { var pos = i << amp; if (i % 4 == 0) { Console.WriteLine("Moving to {0}/{1}", pos, limit << amp); } ForceSendToAllPrimary(busGroup, pos, false); Thread.Sleep(10); } for (; i >= 0; i--) { var pos = i << amp; if (i % 4 == 0) { Console.WriteLine("Moving to {0}/{1}", pos, limit << amp); } ForceSendToAllPrimary(busGroup, pos, false); Thread.Sleep(10); } Console.WriteLine("Wrote {0} messages total", txCountTotal); } Console.WriteLine("Closing bus..."); busGroup.Close(); }
static void Main(string[] args) { GCAN.Initializer.RegisterDevices(); var busGroup = new BusGroup(); Console.WriteLine("Opening bus..."); busGroup.Open(500000); // Refresh the motors (for debugging) Console.WriteLine("Finding motors..."); busGroup.Refresh(80); // Print found motors var foundMotors = busGroup.GetAllMotors(); Console.WriteLine("Found {0} motors : ", foundMotors.Count); foreach (var it in foundMotors) { Console.WriteLine("{0} : {1}", it.Key, it.Value); } if (foundMotors.Count == 0) { busGroup.Close(); return; } // Print gaps in ID range var foundIDs = foundMotors.Keys.ToList(); if (foundMotors.Count > 0) { var startID = foundIDs[0]; var endID = foundIDs[foundMotors.Count - 1]; Console.WriteLine("Missing contiguous IDs between {0} -> {1}:", startID, endID); for (int id = startID; id <= endID; id++) { if (!foundIDs.Contains(id)) { Console.Write("{0}, ", id); } } } Thread.Sleep(2000); // Enable Torque SendToAll(busGroup, Messages.RegisterType.ControlMode, 1, true); // Disable screen (movements are smoother) SendToAll(busGroup, Messages.RegisterType.InterfaceEnabled, 0, true); // Perform movement N times int N = 1000; for (int n = 0; n < N; n++) { Console.WriteLine("Iteration {0}/{1}...", n, N); int i = 0; int limit = 1 << 21; int step = 1 << 10; for (; i < limit; i += step) { var stepStart = DateTime.Now; if (i % 4 == 0) { Console.WriteLine("Moving to {0}/{1}", i, limit); } SendToAllPrimary(busGroup, i, true); var delayTime = 50 - (DateTime.Now - stepStart).TotalMilliseconds; if (delayTime < 0) { Console.WriteLine("T"); } else { Thread.Sleep((int)delayTime); } } for (; i >= 0; i -= step) { if (i % 4 == 0) { Console.WriteLine("Moving to {0}/{1}", i, limit); } SendToAllPrimary(busGroup, i, true); //Thread.Sleep(100); } Console.WriteLine("Wrote {0} messages total", txCountTotal); } Console.WriteLine("Closing bus..."); busGroup.Close(); }
static void Main(string[] args) { UInt64 txCountTotal = 0; UInt64 rxCountTotal = 0; // Also get GCAN devices GCAN.Initializer.RegisterDevices(); var busGroup = new BusGroup(); Console.WriteLine("Opening bus..."); busGroup.Open(500000); if (busGroup.Buses.Count < 1) { throw (new Exception("No buses found")); } var bus = busGroup.Buses[0]; Console.WriteLine("Timestamp : {0}", bus.Channel.Device.Timestamp); // Disable Torque and enable screen (watch the screen to notice messaging) Console.WriteLine("Setting up for run"); busGroup.SetRegisterValueBlind(1, Messages.RegisterType.ControlMode, 0, true); busGroup.SetRegisterValueBlind(1, Messages.RegisterType.InterfaceEnabled, 1, true); var writeRequest = new Messages.WriteRequest(1, Messages.RegisterType.TargetPosition, 0); Console.WriteLine("Sending..."); while (true) { for (int i = 0; i < 100; i++) { // Send to device 1 writeRequest.ID = 1; writeRequest.Value = i << 14; bus.Send(writeRequest, false); txCountTotal += 1; // Send to absent devices for (int j = 0; j < 256; j++) { writeRequest.ID = 2; bus.Send(writeRequest, false); txCountTotal += 1; } } busGroup.BlockUntilActionsComplete(new TimeSpan(0, 0, 10)); busGroup.Update(); rxCountTotal += (UInt64)bus.RxCountThisFrame; Console.WriteLine("Sent {0} frames. Received {1} frames", txCountTotal, rxCountTotal); } Console.WriteLine("Closing bus..."); busGroup.Close(); }
static void Main(string[] args) { // Register GCAN GCAN.Initializer.RegisterDevices(); //// do the same as VVVV example //{ // Console.WriteLine("First try"); // var busGroup = new BusGroup(); // busGroup.Open(500000); // busGroup.Refresh(80); // var motors = busGroup.GetAllMotors(); // Console.WriteLine("Found {0} motors", motors.Count); // busGroup.Close(); //} // do our actual program test { var busGroup = new BusGroup(); Console.WriteLine("Opening bus"); busGroup.Open(500000); Console.WriteLine("Found {0} buses :", busGroup.Buses.Count); foreach (var bus in busGroup.Buses) { Console.WriteLine(bus.DevicePath); } Console.WriteLine(); Console.WriteLine("Finding motors"); Console.WriteLine(); busGroup.Refresh(80); // Print found motors var foundMotors = busGroup.GetAllMotors(); Console.WriteLine("Found {0} motors : ", foundMotors.Count); foreach (var it in foundMotors) { Console.WriteLine(it.Value); } Console.WriteLine(); // Print gaps in ID range var foundIDs = foundMotors.Keys.ToList(); if (foundMotors.Count > 0) { var startID = foundIDs[0]; var endID = foundIDs[foundMotors.Count - 1]; Console.WriteLine("Missing contiguous IDs between {0} -> {1} (if any):", startID, endID); for (int id = startID; id <= endID; id++) { if (!foundIDs.Contains(id)) { Console.Write("{0}, ", id); } } } Console.WriteLine(); var errors = busGroup.GetAllErrors(); foreach (var error in errors) { Console.WriteLine(error); } busGroup.Close(); } }