1. Patrick Plenefisch
  2. ZomB Dashboard System

Source

ZomB Dashboard System / ZomB.DriverStation / DriverStation.cs

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * ZomB Dashboard System <http://firstforge.wpi.edu/sf/projects/zombdashboard>
 * Copyright (C) 2011, Patrick Plenefisch and FIRST Robotics Team 451 "The Cat Attack"
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.ComponentModel;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System451.Communication.Dashboard.Utils;

namespace System451.Communication.Dashboard.Net.DriverStation
{
    [WPF.Design.ZomBControl("Driver Station", Description = "A small driver station for driving the robot without runnning the ds software", IconName = "DSIcon")]
    [TemplatePart(Name = "PART_endis", Type = typeof(Button))]
    [TemplatePart(Name = "PART_Status", Type = typeof(Label))]
    [WPF.Design.ZomBDesignableProperty("Width", Dynamic = true, Category = "Layout")]
    [WPF.Design.ZomBDesignableProperty("Height", Dynamic = true, Category = "Layout")]
    [WPF.Design.ZomBDesignableProperty("Background")]
    [WPF.Design.ZomBDesignableProperty("BorderBrush")]
    [WPF.Design.ZomBDesignableProperty("BorderThickness")]
    public class DriverStation : Control, IZomBControl
    {
        Button PART_endis;
        Label statuslbl;
        DashboardDataHub ddh;
        UdpClient uc;
        bool running;
        System.Timers.Timer tmr;
        short loops;
        public const int MaxConnectionTimeout = 10;
        int connected = MaxConnectionTimeout + 1;


        public DriverStation()
        {
            this.Width = 150;
            this.Height = 100;
            this.Background = Brushes.LightGray;
            Enabled = false;
            Joystick1 = new Joystick();
            Joystick2 = new Joystick();
            Joystick3 = new Joystick();
            Joystick4 = new Joystick();
            KeySafety.Start(Enable, Disable, Disable);
        }

        ~DriverStation()
        {
            try
            {
                Disable();
                Stop();
            }
            catch { }
            try
            {
                uc.Close();
                uc = null;
            }
            catch { }
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            PART_endis = base.GetTemplateChild("PART_endis") as Button;
            PART_endis.Click += delegate
            {
                if (Enabled)
                    Disable();
                else
                    Enable();
            };
            Disable();
            statuslbl = base.GetTemplateChild("PART_Status") as Label;
            statuslbl.Content = "Not Connected";
        }

        public bool Enabled { get; private set; }

        public int Team
        {
            get
            {
                return ddh.Team;
            }
        }

        public void Enable()
        {
            PART_endis.Background = Brushes.Red;
            PART_endis.Content = "Disable";
            Enabled = true;
            if (!running)
                Start();
        }

        public void Disable()
        {
            Enabled = false;
            try
            {
                PART_endis.Background = Brushes.Green;
                PART_endis.Content = "Enable";
            }
            catch { }
        }

        private void Start()
        {
            running = true;
            uc = new UdpClient(1150, AddressFamily.InterNetwork);
            tmr = new System.Timers.Timer(20);
            tmr.AutoReset = true;
            tmr.Elapsed += new ElapsedEventHandler(hz);
            tmr.Start();
            dolisten();
        }

        private void dolisten()
        {
            uc.BeginReceive(delegate(IAsyncResult ar)
            {
                try
                {
                    IPEndPoint iep = null;
                    byte[] dgram = uc.EndReceive(ar, ref iep);
                    connected = 0;
                    stat(dgram[1].ToString("x") + "." + dgram[2].ToString("x"));
                    dolisten();
                }
                catch { }
            }, this);
        }

        private void stat(string stat)
        {
            statuslbl.Dispatcher.Invoke(new VoidFunction(() => statuslbl.Content = stat), null);
        }

        void hz(object sender, ElapsedEventArgs e)
        {
            try
            {
                int start = ((int)(Team / 100.0)) * 100;

                uc.Send(GetStatus(), 1024, "10." + (start / 100) + "." + (Team - start) + ".2", 1110);

                if (connected > MaxConnectionTimeout)
                {
                    stat("Not Connected");
                }
                else
                    connected++;
            }
            catch
            {
                if (tmr != null)
                    tmr.Stop();
                try
                {
                    uc.Close();
                }
                catch { }
                //throw;
            }
        }

        private byte[] GetStatus()
        {
            byte[] r = new byte[1024];
            r[0] = (byte)(loops >> 8);
            r[1] = (byte)(loops);
            loops++;
            var status = new BitField();
            /*
             * 0-FPGA Checksum
             * 1-cRio Checksum
             * 2-Resynch
             * 3-FMS Attached
             * 4-Auton
             * 5-Enabled
             * 6-Not E-Stopped
             * 7-Reset
             */
            status[5] = Enabled;
            status[6] = true;
            r[2] = status.Byte;
            r[3] = 0xff;//digitalInput
            r[4] = (byte)(Team >> 8);
            r[5] = (byte)(Team);
            //alliance, R/B, ascii 1,2,3
            r[6] = 0x52;
            r[7] = 0x31;
            //Joystick data
            try
            {
                Dispatcher.Invoke(new BytesFunction(sendJoystics), r);
            }
            catch (ThreadAbortException) { throw; }
            catch { }


            //version
            r[72] = 0x31;
            r[73] = 0x30;
            r[74] = 0x30;
            r[75] = 0x32;
            r[76] = 0x30;
            r[77] = 0x38;
            r[78] = 0x30;
            r[79] = 0x30;

            //CRC
            uint cr = Libs.Crc32.Compute(r);
            r[1020] = (byte)(cr >> 24);
            r[1021] = (byte)(cr >> 16);
            r[1022] = (byte)(cr >> 8);
            r[1023] = (byte)(cr);
            return r;
        }


        private void sendJoystics(byte[] r)
        {
            Joystick1.SaveDataTo(r, 8);
            Joystick2.SaveDataTo(r, 16);
            Joystick3.SaveDataTo(r, 24);
            Joystick4.SaveDataTo(r, 32);
        }

        private void Stop()
        {
            if (tmr != null)
                tmr.Stop();
            Disable();
            hz(this, null);
            running = false;
        }

        static void joychanged(object o, DependencyPropertyChangedEventArgs e)
        {
            ((Joystick)(o as DriverStation).GetValue(e.Property)).SetFindName((n) => (o as DriverStation).FindName(n));
        }

        [WPF.Design.ZomBDesignable(), Category("IO")]
        public Joystick Joystick1
        {
            get { return (Joystick)GetValue(Joystick1Property); }
            set { SetValue(Joystick1Property, value); }
        }

        // Using a DependencyProperty as the backing store for Joystick1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Joystick1Property =
            DependencyProperty.Register("Joystick1", typeof(Joystick), typeof(DriverStation), new UIPropertyMetadata(null, joychanged));

        [WPF.Design.ZomBDesignable(), Category("IO")]
        public Joystick Joystick2
        {
            get { return (Joystick)GetValue(Joystick2Property); }
            set { SetValue(Joystick2Property, value); }
        }

        // Using a DependencyProperty as the backing store for Joystick1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Joystick2Property =
            DependencyProperty.Register("Joystick2", typeof(Joystick), typeof(DriverStation), new UIPropertyMetadata(null, joychanged));

        [WPF.Design.ZomBDesignable(), Category("IO")]
        public Joystick Joystick3
        {
            get { return (Joystick)GetValue(Joystick3Property); }
            set { SetValue(Joystick3Property, value); }
        }

        // Using a DependencyProperty as the backing store for Joystick1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Joystick3Property =
            DependencyProperty.Register("Joystick3", typeof(Joystick), typeof(DriverStation), new UIPropertyMetadata(null, joychanged));

        [WPF.Design.ZomBDesignable(), Category("IO")]
        public Joystick Joystick4
        {
            get { return (Joystick)GetValue(Joystick4Property); }
            set { SetValue(Joystick4Property, value); }
        }

        // Using a DependencyProperty as the backing store for Joystick1.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty Joystick4Property =
            DependencyProperty.Register("Joystick4", typeof(Joystick), typeof(DriverStation), new UIPropertyMetadata(null, joychanged));

        #region IZomBControl Members

        bool IZomBControl.IsMultiWatch { get { return false; } }

        string IZomBControl.ControlName { get { return null; } }

        void IZomBControl.UpdateControl(string value) { }

        void IZomBControl.ControlAdded(object sender, ZomBControlAddedEventArgs e)
        {
            ddh = e.Controller.GetDashboardDataHub();
            //we only want the controller, now remove ourselves
            ddh.Remove(this);
        }

        #endregion
    }
    /**************************************
     **** Driver Station Protocol 2010 ****
     **************************************
     * The UDP server needs to be set up on
     * port 1150 and set to recieve from 
     * only the robot's IP. The UDP client
     * should send to 1110 on the robot at
     * 50 hertz.
     * 
     * 
     * Values in bytes
Offset - Size - Description - extra info

First, is the Driver Station -> cRio packet

0-2-Packet Index
2-1-Control Byte (more about this one later)
3-1-Driver Station Digital Input
4-2-Team ID
6-1-Driver Station Alliance
7-1-Driver Station Position
8-(1x6)-Joystick 0, 6 bytes, each is one axis (signed byte, -128 to 127)
14-2-Joystick 0 Buttons
16-(1x6)-Joystick 1, 6 bytes, same as Joystick 0
22-2-Joystick 1 Buttons
24-(1x6)-Joystick 2, 6 bytes, same as Joystick 1
30-2-Joystick 2 Buttons
32-(1x6)-Joystick 3, 6 bytes, same as Joystick 2
38-2-Joystick 3 Buttons

40-2-Analog 0-Only uses 10 of the 16 bits, so max value of 1024
42-2-Analog 1-Same as 0
44-2-Analog 2-Same as 0
46-2-Analog 3-Same as 0

48-8-cRio checksum
56-4-FPGA Checksum 1
60-4-FPGA Checksum 2
64-4-FPGA Checksum 3
68-4-FPGA Checksum 4

72-8-Version Data

80-938-High end data, this is where current dashboard sends/recieves data

1020-4-Packet CRC (More on this one later)


Now for cRio -> Driver Station (more simple)

0-1-Control Byte
1-2-Battery Voltage, slightly weird, explained later
3-1-Robot Digital Output
4-4-Unknown/Not Needed
8-2-Team Number
10-6-Robot Mac
16-8-Robot Version
24-6-Unknown
30-2-Counter
32-988-High End Data
1020-4-CRC checksum


Now, for further explanation

Control Byte
the bits of the control byte specify robot state, such as enabled/disabled, auton/teleop, etc
0-FPGA Checksum
1-cRio Checksum
2-Resynch
3-FMS Attached
4-Auton
5-Enabled
6-Not E-Stopped
7-Reset
If you would like help on how to set bits of a number, i can help you with that as well

Packet CRC
make sure that you are generating all 1024 bytes, and that 1020-1024 are empty when you generate this.

Battery voltage
this value was weird, but the value is sent over in the following fashion
if the first byte has a hex value of 12 and the second has a hex value of 45, then the battery is 12.45 volts, so if you were gonna set a label, you would do label.Text = Convert.toString(firstByte,16) + "." + Convert.toString(secondByte,16) + " volts"


     * 
     * 
     **************************************/
}