Imports System.Collections.Concurrent Imports System.Linq Imports System.Threading Imports System.Threading.Tasks Imports System.Timers Imports TIS.Imaging Imports TIS.Imaging.VCDHelpers Imports System.Drawing.Imaging Imports System.ComponentModel Imports System.Drawing.Drawing2D Imports System.Globalization Imports Tinkerforge Imports System.Data.Common Public Class Form1 'TinkerForge Connection Const HOST As String = "10.0.1.48" Const PORT As Integer = 4223 'Lift Grundeinstellungen -------------------------- Const UID_motor As String = "5Xy3ix" ' Change XXYYZZ to the UID of your Stepper Brick Public ipcon_motor As New IPConnection() ' Create IP connection Public stepper_motor As New BrickSilentStepper(UID_motor, ipcon_motor) ' Create device object 'Drehung Grundeinstellungen -------------------------- 'Const UID_motor_dreh As String = "62gzoa" ' Change XXYYZZ to the UID of your Stepper Brick 'Public ipcon_motor_dreh As New IPConnection() ' Create IP connection 'Public stepper_motor_dreh As New BrickStepper(UID_motor_dreh, ipcon_motor_dreh) ' Create device object 'Servo Grundeinstelluingen ---------------------- 'Const UID_servo As String = "SB3" ' Change XXYYZZ to the UID of your Stepper Brick 'Public ipcon_servo As New IPConnection() ' Create IP connection 'Public stepper_servo As New BrickletServoV2(UID_servo, ipcon_servo) ' Create device object 'Mikroschalter Grundeinstellungen ----------------------- Const UID_schalter As String = "Pg1" ' Change XYZ to the UID of your Analog Out Bricklet 3.0 Public volt As Integer Public ipcon_schalter As New IPConnection() ' Create IP connection Public ai_schalter As New BrickletAnalogInV3(UID_schalter, ipcon_schalter) ' Create device object 'IR-Image Grundeinstellungen ------------------------------ 'Const UID_IR As String = "Nbb" ' Change XYZ to the UID of your Thermal Imaging Bricklet 'Public ipcon_IR As New IPConnection() ' Create IP connection 'Public ti_IR As New BrickletThermalImaging(UID_IR, ipcon_IR) ' Create device object 'LED-Display Grundeinstellungen ------------------------------ 'Const UID_LED As String = "JP3" ' Change XYZ to the UID of your Thermal Imaging Bricklet 'Public ipcon_LED As New IPConnection() ' Create IP connection 'Public ti_LED As New BrickletSegmentDisplay4x7V2(UID_LED, ipcon_LED) ' Create device object 'Public rand_x As New Random 'Public rand_y As New Random 'Public zahl_x As Byte 'Public zahl_y As Byte 'Relais_Motor Grundeinstellungen ------------------------------ 'Const UID_Relais As String = "Nov" ' Change XYZ to the UID of your Thermal Imaging Bricklet 'Public ipcon_Relais As New IPConnection() ' Create IP connection 'Public ti_Relais As New BrickletIndustrialDualRelay(UID_Relais, ipcon_Relais) ' Create device object 'Relais_Licht Grundeinstellungen ------------------------------ 'Const UID_Relais_Licht As String = "NqA" ' Change XYZ to the UID of your Thermal Imaging Bricklet 'Public ipcon_Relais_Licht As New IPConnection() ' Create IP connection 'Public ti_Relais_Licht As New BrickletIndustrialDualRelay(UID_Relais_Licht, ipcon_Relais_Licht) ' Create device object Public cts As CancellationTokenSource Private Async Sub cmd_rauf_Click(sender As Object, e As EventArgs) Handles cmd_rauf.Click cts = New CancellationTokenSource() 'Tinkerforge 'ti_Relais.SetValue(True, False) 'Call motor_rauf_sync() 'sync .... Programm friert ein .... Lift bleibt nicht stehen Try Await Task.Run(Sub() motor_rauf_async(cts.Token)) Catch ex As OperationCanceledException MsgBox("Download canceled.") End Try End Sub Private Async Function motor_runter_async(ct As CancellationToken) As Task(Of Integer) 'Try 'ipcon_motor.Connect(HOST, PORT) ' Connect to brickd ' Don't use device before ipcon is connected 'Catch 'End Try stepper_motor.SetMotorCurrent(800) ' 800mA stepper_motor.SetStepConfiguration(BrickSilentStepper.STEP_RESOLUTION_8, True) stepper_motor.SetMaxVelocity(4200) ' Velocity 2000 steps/s 'stepper_motor.SetSyncRect(True) 'stepper_motor.SetDecay(60000) ' Slow acceleration (500 steps/s^2), ' Fast deacceleration (5000 steps/s^2) stepper_motor.SetSpeedRamping(5000, 5000) stepper_motor.Enable() Thread.Sleep(500) stepper_motor.DriveBackward() Do Until volt > 2 'txt_Motor_Lift_Pos.Text = stepper_motor.GetCurrentPosition Loop 'Exit Function If volt = 99 Then Exit Function stepper_motor.Stop() Thread.Sleep(1000) stepper_motor.DriveForward() Thread.Sleep(1500) stepper_motor.Stop() 'stepper_motor.Disable() 'ipcon_motor.Disconnect() Return 1 'um iregendetwas zu retournieren End Function Private Async Function motor_rauf_async(ct As CancellationToken) As Task(Of Integer) stepper_motor.SetMotorCurrent(800) ' 800mA stepper_motor.SetStepConfiguration(BrickSilentStepper.STEP_RESOLUTION_8, True) stepper_motor.SetMaxVelocity(4200) ' Velocity 2000 steps/s 'stepper_motor.SetSyncRect(True) 'stepper_motor.SetDecay(60000) ' Slow acceleration (500 steps/s^2), ' Fast deacceleration (5000 steps/s^2) stepper_motor.SetSpeedRamping(5000, 5000) stepper_motor.Enable() 'stepper_motor.SetCurrentPosition(0) 'Thread.Sleep(500) stepper_motor.DriveForward() Do Until volt > 4 'txt_Motor_Lift_Pos.Text = stepper_motor.GetCurrentPosition Loop 'Exit Function If volt = 99 Then Exit Function stepper_motor.Stop() Thread.Sleep(1000) '500 stepper_motor.DriveBackward() Thread.Sleep(2000) stepper_motor.Stop() Thread.Sleep(250) stepper_motor.SetCurrentPosition(-1) 'nie 0 !!!! 'txt_Motor_Lift_Pos.Text = stepper_motor.GetCurrentPosition 'stepper_motor.Disable() 'ipcon_motor.Disconnect() Return 1 'um iregendetwas zu retournieren End Function Private Async Sub cmd_runter_Click(sender As Object, e As EventArgs) Handles cmd_runter.Click cts = New CancellationTokenSource() 'Tinkerforge 'ti_Relais.SetValue(True, False) Try Await Task.Run(Sub() motor_runter_async(cts.Token)) Catch ex As OperationCanceledException MsgBox("Download canceled.") End Try End Sub Sub VoltageCB(ByVal sender As BrickletAnalogInV3, ByVal mV As Integer) 'txt_Mikroschalter.Text = voltage / 1000.0 'volt = mV / 1000 'Exit Sub Try volt = mV / 1000 Catch MsgBox("AKR: Mikroschalter-Fehler - Transporter aus- und einschalten!") : Me.Close() End Try End Sub Private Sub cmdreset_Click(sender As Object, e As EventArgs) Handles cmdreset.Click If cts IsNot Nothing Then cts.Cancel() 'cts.Dispose() 'bringt nix End If volt = 99 'Tinkerforge stepper_motor.Stop() Thread.Sleep(250) 'Task.CompletedTask.Dispose() 'bringt nix End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load ipcon_motor.Connect(HOST, PORT) AddHandler ipcon_motor.EnumerateCallback, AddressOf EnumerateCB ipcon_motor.Enumerate() 'ipcon_servo.Connect(HOST, PORT) 'AddHandler stepper_servo.PositionReachedCallback, AddressOf PositionReachedCB 'ipcon_servo.Enumerate() ' Enable position reached callback 'stepper_servo.SetPositionReachedCallbackConfiguration(0, True) 'MikroSchalter---------------------- ipcon_schalter.Connect(HOST, PORT) 'ai_schalter.SetVoltageCallbackConfiguration(100, False, "x"c, 2500, 3500) '"Fehlermeldung: Mikro-Schalter Did not receive response in time for function ID 1" ai_schalter.SetVoltageCallbackConfiguration(1000, False, "x"c, 2500, 3500) 'Probe ob Fehlermeldung nicht mehr kommt !?!? 'Parameter: 'period – Typ: Long, Einheit: 1 ms, Wertebereich: [0 bis 232 - 1], Standardwert: 0 'valueHasToChange – Typ: Boolean, Standardwert: false 'Option On– Typ: Char, Wertebereich: Siehe Konstanten, Standardwert: "x"C 'min – Typ: Integer, Einheit: 1 mV, Wertebereich: [0 bis 216 - 1], Standardwert: 0 'max – Typ: Integer, Einheit: 1 mV, Wertebereich: [0 bis 216 - 1], Standardwert: 0 AddHandler ipcon_schalter.EnumerateCallback, AddressOf EnumerateCB AddHandler ai_schalter.VoltageCallback, AddressOf VoltageCB ipcon_schalter.Enumerate() ipcon_schalter.SetTimeout(1000) 'ipcon_IR.Connect(HOST, PORT) 'AddHandler ipcon_IR.EnumerateCallback, AddressOf EnumerateCB 'ipcon_IR.Enumerate() 'SYN 'Call mot_dreh_kalib() 'ASYN 'eigentlich sollte async Aufruf sein 'Await Task.Run(Sub() 'Call mot_dreh_kalib() ' 'ti_Relais.SetValue(True, False) 'ti_Relais.SetValue(False, False) End Sub Sub EnumerateCB(ByVal sender As IPConnection, ByVal uid As String, ByVal connectedUid As String, ByVal position As Char, ByVal hardwareVersion() As Short, ByVal firmwareVersion() As Short, ByVal deviceIdentifier As Integer, ByVal enumerationType As Short) 'System.Console.WriteLine("UID: {0}", uid) 'System.Console.WriteLine("Enumeration Type: {0}", enumerationType) If enumerationType = IPConnection.ENUMERATION_TYPE_DISCONNECTED Then 'System.Console.WriteLine("") Beep() Return End If 'System.Console.WriteLine("Connected UID: {0}", connectedUid) 'System.Console.WriteLine("Position: {0}", position) 'System.Console.WriteLine("Hardware Version: {0}.{1}.{2}", hardwareVersion(0), hardwareVersion(1), hardwareVersion(2)) 'System.Console.WriteLine("Firmware Version: {0}.{1}.{2}", firmwareVersion(0), firmwareVersion(1), firmwareVersion(2)) 'System.Console.WriteLine("Device Identifier: {0}", deviceIdentifier) 'System.Console.WriteLine("") End Sub Private Sub PositionReachedCB(ByVal sender As BrickletServoV2, ByVal servoChannel As Integer, ByVal position As Short) If position = 9000 Then 'Console.WriteLine("Position: 90°, going to -90°") sender.SetPosition(servoChannel, -9000) ElseIf position = -9000 Then 'Console.WriteLine("Position: -90°, going to 90°") sender.SetPosition(servoChannel, 9000) Else ' Can only happen if another program sets position ' Console.WriteLine("Error") End If End Sub End Class