-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSRV1.cs
1243 lines (1114 loc) · 48.8 KB
/
SRV1.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// AForge Surveyor Robotics Library
// AForge.NET framework
// http://www.aforgenet.com/framework/
//
// Copyright © AForge.NET, 2005-2011
//
namespace AForge.Robotics.Surveyor
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using AForge;
/// <summary>
/// Manipulation of Surveyor SRV-1 Blackfin robot/camera.
/// </summary>
///
/// <remarks>
/// <para>The class allows to manipulate with <a href="http://www.surveyor.com/SRV_info.html">Surveyor SRV-1 Blackfin Robot</a>
/// - getting video from its camera, manipulating motors and servos,
/// reading ultrasonic modules' values, sending direct commands, etc.</para>
///
/// <para><img src="img/robotics/srv1-robot.jpg" width="240" height="216" /></para>
///
/// <para>Sample usage:</para>
/// <code>
/// SRV1 srv = new SRV1( );
/// // connect to SRV-1 robot
/// srv.Connect( "169.254.0.10", 10001 );
/// // stop motors
/// srv.StopMotors( );
/// // set video resolution and quality
/// srv.SetQuality( 7 );
/// srv.SetResolution( SRV1.VideoResolution.Small );
/// // get version string
/// string version = srv.GetVersion( );
///
/// // get robot's camera
/// SRV1Camera camera = srv.GetCamera( );
///
/// // set NewFrame event handler
/// camera.NewFrame += new NewFrameEventHandler( video_NewFrame );
/// // start the video source
/// camera.Start( );
/// // ...
///
/// private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
/// {
/// // get new frame
/// Bitmap bitmap = eventArgs.Frame;
/// // process the frame
/// }
/// </code>
/// </remarks>
///
/// <seealso cref="SRV1Camera"/>
///
public class SRV1
{
// dummy object to lock for synchronization
private object sync = new object( );
/// <summary>
/// Enumeration of predefined motors' commands.
/// </summary>
///
/// <remarks><para>This enumeration defines set of motors' commands, which can
/// be executed using <see cref="ControlMotors"/> method.</para>
///
/// <para><note>Controlling SRV-1 motors with these commands is only possible
/// after at least one direct motor command is sent, which is done using <see cref="StopMotors"/> or
/// <see cref="RunMotors"/> methods.</note></para>
///
/// <para><note>The <b>IncreaseSpeed</b> and <b>DecreaseSpeed</b> commands do not have any effect
/// unless another driving command is sent. In other words, these do not increase/decrease speed of
/// current operation, but affect speed of all following commands.</note></para>
///
/// <para><note>The <b>RotateLeft</b> and <b>RotateRight</b> commands may be useful only for the original
/// <a href="http://www.surveyor.com/SRV_info.html">Surveyor SRV-1 Blackfin Robot</a>.
/// For most of other robots, which may have different motors and moving base, these commands
/// will not be accurate – will not rotate for 20 degrees.
/// </note></para>
/// </remarks>
///
public enum MotorCommand
{
/// <summary>
/// Robot drive forward.
/// </summary>
DriveForward = '8',
/// <summary>
/// Robot drive back.
/// </summary>
DriveBack = '2',
/// <summary>
/// Robot drive left.
/// </summary>
DriveLeft = '4',
/// <summary>
/// Robot drive right.
/// </summary>
DriveRight = '6',
/// <summary>
/// Robot drift left.
/// </summary>
DriftLeft = '7',
/// <summary>
/// Robot drift right.
/// </summary>
DriftRight = '9',
/// <summary>
/// Robot stop.
/// </summary>
Stop = '5',
/// <summary>
/// Robot drive back and right.
/// </summary>
DriveBackRight = '3',
/// <summary>
/// Robot drive back and left.
/// </summary>
DriveBackLeft = '1',
/// <summary>
/// Robot rotate left 20 degrees.
/// </summary>
///
RotateLeft = '0',
/// <summary>
/// Robot rotate right 20 degrees.
/// </summary>
///
RotateRight = '.',
/// <summary>
/// Increase motors' speed.
/// </summary>
///
IncreaseSpeed = '+',
/// <summary>
/// Decrease motors' speed.
/// </summary>
///
DecreaseSpeed = '-',
}
/// <summary>
/// Enumeration of Surveyor SRV-1 Blackfin cameras resolutions.
/// </summary>
public enum VideoResolution
{
/// <summary>
/// 160x120
/// </summary>
Tiny = 'a',
/// <summary>
/// 320x240
/// </summary>
Small = 'b',
/// <summary>
/// 640x480
/// </summary>
Medium = 'c',
/// <summary>
/// 1280x1024
/// </summary>
Large = 'd'
}
private IPEndPoint endPoint = null;
// Connecton end-point
internal IPEndPoint EndPoint
{
get { return endPoint; }
}
// socket used for communication with SVS
Socket socket = null;
// background communicaton thread
private Thread thread = null;
// event signaling thread to exit
private ManualResetEvent stopEvent = null;
// event signaling about available request in communication queue
private AutoResetEvent requestIsAvailable;
// event sugnaling about available response
private AutoResetEvent replyIsAvailable;
// last processed request which requires reply
private CommunicationRequest lastRequestWithReply;
// communication request
private class CommunicationRequest
{
public byte[] Request;
public byte[] ResponseBuffer;
public int BytesRead; // -1 on error
public CommunicationRequest( byte[] request )
{
this.Request = request;
}
public CommunicationRequest( byte[] request, byte[] responseBuffer )
{
this.Request = request;
this.ResponseBuffer = responseBuffer;
}
}
// communication queue
Queue<CommunicationRequest> communicationQueue = new Queue<CommunicationRequest>( );
// SRV-1 camera
private SRV1Camera camera;
/// <summary>
/// SRV-1 host address.
/// </summary>
///
/// <remarks><para>The property keeps SRV-1 IP address if the class is connected
/// to SRV-1 Blackfin robot/camera, otherwise it equals to <see langword="null."/>.</para></remarks>
///
public string HostAddress
{
get { return ( endPoint == null ) ? null : endPoint.Address.ToString( ); }
}
/// <summary>
/// SRV-1 port number.
/// </summary>
///
/// <remarks><para>The property keeps SRV-1 port number if the class is connected
/// to SRV-1 Blackfin robot/camera, otherwise it equals to 0.</para></remarks>
///
public int Port
{
get { return ( endPoint == null ) ? 0 : endPoint.Port; }
}
/// <summary>
/// Connection state.
/// </summary>
///
/// <remarks><para>The property equals to <see langword="true"/> if the class is connected
/// to SRV-1 Blackfin robot/camera, otherwise it equals to <see langword="false"/>.</para>
///
/// <para><note>The property is not updated by the class, when connection was lost or
/// communication failure was detected (which results into <see cref="ConnectionLostException"/>
/// exception). The property only shows status of <see cref="Connect"/> method.</note></para>
/// </remarks>
///
public bool IsConnected
{
get { return ( endPoint != null ); }
}
/// <summary>
/// Initializes a new instance of the <see cref="SRV1"/> class.
/// </summary>
///
public SRV1( )
{
}
/// <summary>
/// Connect to SRV-1 Blackfin robot/camera.
/// </summary>
///
/// <param name="ip">IP address of SRV-1 robot.</param>
/// <param name="port">Port number to connect to.</param>
///
/// <remarks><para>The method establishes connection to SRV-1 Blackfin robot/camera.
/// If it succeeds then other methods can be used to manipulate the robot.</para>
///
/// <para><note>The method calls <see cref="Disconnect"/> before making any connection
/// attempts to make sure previous connection is closed.</note></para>
/// </remarks>
///
/// <exception cref="ConnectionFailedException">Failed connecting to SRV-1.</exception>
///
public void Connect( string ip, int port )
{
Disconnect( );
lock ( sync )
{
try
{
// make sure communication queue is empty
communicationQueue.Clear( );
endPoint = new IPEndPoint( IPAddress.Parse( ip ), Convert.ToInt16( port ) );
// create TCP/IP socket and set timeouts
socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
socket.ReceiveTimeout = 5000;
socket.SendTimeout = 1000;
// connect to SVS
socket.Connect( endPoint );
// create events
stopEvent = new ManualResetEvent( false );
requestIsAvailable = new AutoResetEvent( false );
replyIsAvailable = new AutoResetEvent( false );
// create and start new thread
thread = new Thread( new ThreadStart( CommunicationThread ) );
thread.Start( );
}
catch ( SocketException )
{
socket.Close( );
socket = null;
endPoint = null;
throw new ConnectionFailedException( "Failed connecting to SRV-1." );
}
}
}
/// <summary>
/// Disconnect from SRV-1 Blackfin robot.
/// </summary>
///
/// <remarks><para>The method disconnects from SRV-1 robot making all other methods
/// unavailable (except <see cref="Connect"/> method). In the case if user
/// obtained instance of camera using <see cref="GetCamera"/> method, the video will
/// be stopped automatically (and those <see cref="SRV1Camera"/> instances should be discarded).
/// </para></remarks>
///
public void Disconnect( )
{
lock ( sync )
{
if ( thread != null )
{
// signal camera to stop
if ( camera != null )
{
camera.SignalToStop( );
}
// signal worker thread to stop
stopEvent.Set( );
requestIsAvailable.Set( );
replyIsAvailable.Set( );
// finilze the camera
if ( camera != null )
{
// wait for aroung 250 ms
for ( int i = 0; ( i < 5 ) && ( camera.IsRunning ); i++ )
{
System.Threading.Thread.Sleep( 50 );
}
// abort camera if it can not be stopped
if ( camera.IsRunning )
{
camera.Stop( );
}
camera = null;
}
// wait for aroung 1 s
for ( int i = 0; ( i < 20 ) && ( thread.Join( 0 ) == false ); i++ )
{
System.Threading.Thread.Sleep( 50 );
}
// abort thread if it can not be stopped
if ( thread.Join( 0 ) == false )
{
thread.Abort( );
}
thread = null;
// release events
stopEvent.Close( );
stopEvent = null;
requestIsAvailable.Close( );
requestIsAvailable = null;
replyIsAvailable.Close( );
replyIsAvailable = null;
}
if ( socket != null )
{
if ( socket.Connected )
{
socket.Disconnect( false );
}
socket.Close( );
socket = null;
endPoint = null;
}
}
}
// Try to reconnect to SVS
private void Reconnect( )
{
if ( socket != null )
{
if ( socket.Connected )
{
socket.Disconnect( false );
}
socket.Close( );
// create TCP/IP socket and set timeouts
socket = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
socket.ReceiveTimeout = 5000;
socket.SendTimeout = 1000;
// connect to SVS
socket.Connect( endPoint );
}
}
/// <summary>
/// Get camera object for the SRV-1 Blackfin robot/camera.
/// </summary>
///
/// <returns>Returns <see cref="SRV1Camera"/> object, which is connected to SRV1 Blackfin camera.
/// Use <see cref="SRV1Camera.Start"/> method to start the camera and start receiving video
/// frames it.</returns>
///
/// <remarks><para>The method provides an instance of <see cref="SRV1Camera"/>, which can be used
/// for receiving continuous video frames from the SRV-1 Blackfin camera.
/// In the case if only one image is required, the <see cref="GetImage"/> method can be used.</para>
///
/// <para>Sample usage:</para>
/// <code>
/// // get SRV-1 camera
/// SRV1Camera camera = srv.GetCamera( );
/// // set NewFrame event handler
/// camera.NewFrame += new NewFrameEventHandler( video_NewFrame );
/// // start the video source
/// camera.Start( );
/// // ...
///
/// private void video_NewFrame( object sender, NewFrameEventArgs eventArgs )
/// {
/// // get new frame
/// Bitmap bitmap = eventArgs.Frame;
/// // process the frame
/// }
/// </code>
/// </remarks>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 robot/camera
/// before using this method.</exception>
///
public SRV1Camera GetCamera( )
{
lock ( sync )
{
if ( socket == null )
{
// handle error
throw new NotConnectedException( "Not connected to SRV-1." );
}
if ( camera == null )
{
camera = new SRV1Camera( this );
}
return camera;
}
}
/// <summary>
/// Enqueue communication request.
/// </summary>
///
/// <param name="request">Array of bytes (command) to send to SRV-1 Blackfin robot/camera.</param>
///
/// <remarks><para>The method puts specified command into communication queue and leaves
/// immediately. Once internal communication thread becomes free from sending/receiving previous
/// commands/replies, it will send the queued command.</para>
///
/// <para>The method is useful for those SRV-1 commands, which does not assume any response data
/// in the command's reply.</para>
///
/// <para><note>Since the method only queues a communication request, it does not provide any status
/// of request's delivery and it does not generate any exceptions on failure.</note></para>
/// </remarks>
///
public void Send( byte[] request )
{
lock ( communicationQueue )
{
communicationQueue.Enqueue( new CommunicationRequest( request ) );
}
if ( requestIsAvailable != null )
{
requestIsAvailable.Set( );
}
}
/// <summary>
/// Enqueue communication request and wait for reply.
/// </summary>
///
/// <param name="request">Array of bytes (command) to send to SRV-1 Blackfin robot/camera.</param>
/// <param name="responseBuffer">Buffer to read response into.</param>
///
/// <returns>Returns total bytes read into the response buffer.</returns>
///
/// <remarks><para>The method puts specified command into communication queue and waits until
/// the command is sent to SRV-1 Blackfin robot and reply is received.</para>
///
/// <para><note>If SRV-1 responds with more data than response buffer can fit, then
/// the response buffer will take all the data it can store, but the rest of response
/// will be discarded. The only exception is image request - if response buffer is too
/// small to fit image response, then <see cref="IndexOutOfRangeException"/> exception
/// is thrown. It is user's responsibility to provide response buffer of the correct
/// size. Check definition of the <a href="http://www.surveyor.com/SRV_protocol.html">SRV-1
/// Control Protocol</a> for information about supported commands and responses.</note></para>
/// </remarks>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure.</exception>
/// <exception cref="IndexOutOfRangeException">Response buffer is too small.</exception>
///
public int SendAndReceive( byte[] request, byte[] responseBuffer )
{
lock ( sync )
{
if ( socket == null )
{
// handle error
throw new NotConnectedException( "Not connected to SRV-1." );
}
lock ( communicationQueue )
{
communicationQueue.Enqueue( new CommunicationRequest( request, responseBuffer ) );
}
requestIsAvailable.Set( );
// waiting for reply
replyIsAvailable.WaitOne( );
// no reply since we got disconnect request from user - background thread is exiting
if ( lastRequestWithReply == null )
return 0;
// get number of bytes read
int bytesRead = lastRequestWithReply.BytesRead;
// clean the last reply
lastRequestWithReply = null;
if ( bytesRead == -1 )
{
// handle error
throw new ConnectionLostException( "Connection lost or communicaton failure." );
}
if ( bytesRead == -2 )
{
// handle error
throw new IndexOutOfRangeException( "Response buffer is too small." );
}
return bytesRead;
}
}
/// <summary>
/// Get single image from the SRV-1 Blackfin camera.
/// </summary>
///
/// <returns>Returns image received from the SRV-1 Blackfin camera or <see langword="null"/>
/// if failed decoding provided response.</returns>
///
/// <remarks><para>The method provides single video frame retrieved from the SRV-1 Blackfin
/// camera. However in many cases it is required to receive video frames one after another, so
/// the <see cref="GetCamera"/> method is more preferred for continuous video frames.</para></remarks>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 before using
/// this method.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure. Try to reconnect.</exception>
///
public Bitmap GetImage( )
{
Bitmap image = null;
// buffer to read image into
byte[] buffer = new byte[768 * 1024];
// request image
int bytesRead = SendAndReceive( new byte[] { (byte) 'I' }, buffer );
if ( bytesRead > 10 )
{
// check for image reply signature
if (
( buffer[0] == (byte) '#' ) &&
( buffer[1] == (byte) '#' ) &&
( buffer[2] == (byte) 'I' ) &&
( buffer[3] == (byte) 'M' ) &&
( buffer[4] == (byte) 'J' ) )
{
// extract image size
int imageSize = System.BitConverter.ToInt32( buffer, 6 );
try
{
// decode image from memory stream
image = (Bitmap) Bitmap.FromStream( new MemoryStream( buffer, 10, imageSize ) );
}
catch
{
image = null;
}
}
}
return image;
}
/// <summary>
/// Get SRV-1 firmware version string.
/// </summary>
///
/// <returns>Returns SRV-1 version string.</returns>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 before using
/// this method.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure. Try to reconnect.</exception>
///
public string GetVersion( )
{
byte[] response = new byte[100];
int read = SendAndReceive( new byte[] { (byte) 'V' }, response );
string str = System.Text.ASCIIEncoding.ASCII.GetString( response, 0, read );
str = str.Replace( "##Version -", "" );
str = str.Trim( );
return str;
}
/// <summary>
/// Get SRV-1 running time.
/// </summary>
///
/// <returns>Returns SRV-1 running time in milliseconds.</returns>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 before using
/// this method.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure. Try to reconnect.</exception>
/// <exception cref="ApplicationException">Failed parsing response from SRV-1.</exception>
///
public long GetRunningTime( )
{
byte[] response = new byte[100];
int read = SendAndReceive( new byte[] { (byte) 't' }, response );
string str = System.Text.ASCIIEncoding.ASCII.GetString( response, 0, read );
str = str.Replace( "##time - millisecs:", "" );
str = str.Trim( );
try
{
return long.Parse( str );
}
catch
{
throw new ApplicationException( "Failed parsing response from SRV-1." );
}
}
/// <summary>
/// Run motors connected to SRV-1 robot.
/// </summary>
///
/// <param name="leftSpeed">Left motor's speed, [-127, 127].</param>
/// <param name="rightSpeed">Right motor's speed, [-127, 127].</param>
/// <param name="duration">Time duration to run motors measured in number
/// of 10 milliseconds (0 for infinity), [0, 255].</param>
///
/// <remarks><para>The method provides direct access to motors setting specified,
/// speed to both motors connected to the SRV-1 robot. The maximum absolute speed
/// equals to 127, but the sign specifies direction of motor's rotation (forward or backward).
/// </para>
///
/// <para><note>The method sends 'Mabc' SRV-1 command (see <a href="http://www.surveyor.com/SRV_protocol.html">SRV-1
/// Control Protocol</a>), which uses 2<sup>nd</sup> and 3<sup>rd</sup> timers for
/// controlling motors/servos.</note></para>
/// </remarks>
///
public void RunMotors( int leftSpeed, int rightSpeed, int duration )
{
// check limits
if ( leftSpeed == -128 )
leftSpeed = -127;
if ( rightSpeed == -128 )
rightSpeed = -127;
if ( duration > 255 )
duration = 255;
Send( new byte[] { (byte) 'M', (byte) leftSpeed, (byte) rightSpeed, (byte) duration } );
}
/// <summary>
/// Stop both motors.
/// </summary>
///
/// <remarks><para>The method stops both motors connected to the SRV-1 robot by calling
/// <see cref="RunMotors"/> method specifying 0 for motors' speed.</para></remarks>
///
public void StopMotors( )
{
RunMotors( 0, 0, 0 );
}
/// <summary>
/// Enables fail safe mode - setting motors' speed after timeout.
/// </summary>
///
/// <param name="leftSpeed">Left motor's speed, [-127, 127].</param>
/// <param name="rightSpeed">Right motor's speed, [-127, 127].</param>
///
/// <remarks><para>In the case if fail safe mode is enabled and no commands are received
/// by SRV-1 robot withing 2 seconds, motors' speed will be set to the specified values. The command
/// is very useful to instruct robot to stop if no other commands were sent
/// within 2 last seconds (probably lost connection).</para></remarks>
///
public void EnableFailsafeMode( int leftSpeed, int rightSpeed )
{
// check limits
if ( leftSpeed == -128 )
leftSpeed = -127;
if ( rightSpeed == -128 )
rightSpeed = -127;
Send( new byte[] { (byte) 'F', (byte) leftSpeed, (byte) rightSpeed } );
}
/// <summary>
/// Disable fail safe mode.
/// </summary>
///
/// <remarks><para>The method disable fail safe mode, which was set using
/// <see cref="EnableFailsafeMode"/> method.</para></remarks>
///
public void DisableFailsafeMode( )
{
Send( new byte[] { (byte) 'f' } );
}
/// <summary>
/// Direct servos control of SRV-1 robot.
/// </summary>
///
/// <param name="leftServo">Left servo setting, [0, 100].</param>
/// <param name="rightServo">Right servo setting, [0, 100].</param>
///
/// <remarks><para>Servo settings represent timing pulse widths ranging
/// from 1ms to 2ms. 0 corresponds to a 1ms pulse, 100 corresponds to a 2ms pulse,
/// and 50 is midrange with a 1.5ms pulse.</para>
///
/// <para><note>The method sends 'sab' SRV-1 command (see <a href="http://www.surveyor.com/SRV_protocol.html">SRV-1
/// Control Protocol</a>), which controls 2<sup>nd</sup> bank of servos
/// using 6<sup>th</sup> and 7<sup>th</sup> timers.</note></para>
/// </remarks>
///
public void ControlServos( int leftServo, int rightServo )
{
// check limts
if ( leftServo > 100 )
leftServo = 100;
if ( rightServo > 100 )
rightServo = 100;
Send( new byte[] { (byte) 's', (byte) leftServo, (byte) rightServo } );
}
/// <summary>
/// Control SRV-1 robot's motors using predefined commands.
/// </summary>
///
/// <param name="command">Motor command to send to the SRV-1 Blackfin robot.</param>
///
/// <remarks><para><note>Controlling SRV-1 motors with this method is only available
/// after at least one direct motor command is sent, which is done using <see cref="StopMotors"/> or
/// <see cref="RunMotors"/> methods.</note></para></remarks>
///
public void ControlMotors( MotorCommand command )
{
Send( new byte[] { (byte) command } );
}
/// <summary>
/// Set video quality.
/// </summary>
///
/// <param name="quality">Video quality to set, [1, 8].</param>
///
/// <remarks><para>The method sets video quality, which is specified in [1, 8] range - 1 is
/// the highest quality level, 8 is the lowest quality level.</para>
///
/// <para><note>Setting higher quality level and <see cref="SetResolution">resolution</see>
/// may increase delays for other requests sent to SRV-1. So if
/// robot is used not only for video, but also for controlling servos/motors, and higher
/// response level is required, then do not set very high quality and resolution.
/// </note></para>
/// </remarks>
///
/// <exception cref="ArgumentOutOfRangeException">Invalid quality level was specified.</exception>
///
public void SetQuality( int quality )
{
if ( ( quality < 1 ) || ( quality > 8 ) )
throw new ArgumentOutOfRangeException( "Invalid quality level was specified." );
Send( new byte[] { (byte) 'q', (byte) ( quality + (byte) '0' ) } );
}
/// <summary>
/// Set video resolution.
/// </summary>
///
/// <param name="resolution">Video resolution to set.</param>
///
/// <remarks>
/// <para><note>Setting higher <see cref="SetQuality">quality level</see> and resolution
/// may increase delays for other requests sent to SRV-1. So if
/// robot is used not only for video, but also for controlling servos/motors, and higher
/// response level is required, then do not set very high quality and resolution.
/// </note></para>
/// </remarks>
///
/// <exception cref="ArgumentOutOfRangeException">Invalid resolution was specified.</exception>
///
public void SetResolution( VideoResolution resolution )
{
if ( !Enum.IsDefined( typeof( VideoResolution ), resolution ) )
{
throw new ArgumentException( "Invalid resolution was specified." );
}
Send( new byte[] { (byte) resolution } );
}
/// <summary>
/// Flip video capture or not (for use with upside-down camera).
/// </summary>
///
/// <param name="isFlipped">Specifies if video should be flipped (<see langword="true"/>),
/// or not (<see langword="false"/>).</param>
///
public void FlipVideo( bool isFlipped )
{
Send( new byte[] { (byte) ( ( isFlipped ) ? 'y' : 'Y' ) } );
}
/// <summary>
/// Ping ultrasonic ranging modules.
/// </summary>
///
/// <returns>Returns array of ranges (distances) obtained from ultrasonic sensors. The ranges
/// are measured in inches.</returns>
///
/// <remarks><para>The method sends 'p' SRV-1 command (see <a href="http://www.surveyor.com/SRV_protocol.html">SRV-1
/// Control Protocol</a>), which gets values from ultrasonic ranging modules attached to
/// pins 27, 28, 29, 30 with trigger on pin 18. Supports Maxbotics EZ0 and EZ1 ultrasonic modules.
/// </para></remarks>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 before using
/// this method.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure. Try to reconnect.</exception>
/// <exception cref="ApplicationException">Failed parsing response from SRV-1.</exception>
///
public float[] UltrasonicPing( )
{
byte[] response = new byte[100];
int read = SendAndReceive( new byte[] { (byte) 'p' }, response );
string str = System.Text.ASCIIEncoding.ASCII.GetString( response, 0, read );
str = str.Replace( "##ping ", "" );
str = str.Trim( );
// split string into separate values
string[] strs = str.Split( ' ' );
try
{
float[] distance = new float[4];
for ( int i = 0; i < 4; i++ )
{
distance[i] = (float) int.Parse( strs[i] ) / 100f;
}
return distance;
}
catch
{
throw new ApplicationException( "Failed parsing response from SRV-1." );
}
}
/// <summary>
/// Read byte from I2C device.
/// </summary>
///
/// <param name="deviceID">I2C device ID (7 bit notation).</param>
/// <param name="register">I2C device register to read.</param>
///
/// <returns>Returns byte read from the specified register of the specified I2C device.</returns>
///
/// <para><note>The IC2 device ID should be specified in 7 bit notation. This means that low bit of the ID
/// is not used for specifying read/write mode as in 8 bit notation. For example, if I2C device IDs are 0x44 for reading
/// and 0x45 for writing in 8 bit notation, then it equals to 0x22 device ID in 7 bit notation.
/// </note></para>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 before using
/// this method.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure. Try to reconnect.</exception>
/// <exception cref="ApplicationException">Failed parsing response from SRV-1.</exception>
///
public byte I2CReadByte( byte deviceID, byte register )
{
byte[] response = new byte[100];
int read = SendAndReceive( new byte[] { (byte) 'i', (byte) 'r', deviceID, register }, response );
string str = System.Text.ASCIIEncoding.ASCII.GetString( response, 0, read );
try
{
str = str.Trim( );
// split string into separate values
string[] strs = str.Split( ' ' );
return byte.Parse( strs[1] );
}
catch
{
throw new ApplicationException( "Failed parsing response from SRV-1." );
}
}
/// <summary>
/// Read word from I2C device.
/// </summary>
///
/// <param name="deviceID">I2C device ID (7 bit notation).</param>
/// <param name="register">I2C device register to read.</param>
///
/// <returns>Returns word read from the specified register of the specified I2C device.</returns>
///
/// <para><note>The IC2 device ID should be specified in 7 bit notation. This means that low bit of the ID
/// is not used for specifying read/write mode as in 8 bit notation. For example, if I2C device IDs are 0x44 for reading
/// and 0x45 for writing in 8 bit notation, then it equals to 0x22 device ID in 7 bit notation.
/// </note></para>
///
/// <exception cref="NotConnectedException">Not connected to SRV-1. Connect to SRV-1 before using
/// this method.</exception>
/// <exception cref="ConnectionLostException">Connection lost or communicaton failure. Try to reconnect.</exception>
/// <exception cref="ApplicationException">Failed parsing response from SRV-1.</exception>
///
public ushort I2CReadWord( byte deviceID, byte register )
{
byte[] response = new byte[100];
int read = SendAndReceive( new byte[] { (byte) 'i', (byte) 'R', deviceID, register }, response );
string str = System.Text.ASCIIEncoding.ASCII.GetString( response, 0, read );
try
{
str = str.Trim( );
// split string into separate values
string[] strs = str.Split( ' ' );
return ushort.Parse( strs[1] );
}
catch
{
throw new ApplicationException( "Failed parsing response from SRV-1." );
}
}