C# Class AForge.Robotics.Surveyor.SRV1

Manipulation of Surveyor SRV-1 Blackfin robot/camera.

The class allows to manipulate with Surveyor SRV-1 Blackfin Robot - getting video from its camera, manipulating motors and servos, reading ultrasonic modules' values, sending direct commands, etc.

Sample usage:

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 }
Show file Open project: holisticware-admin/MonoVersal.AForgeNET Class Usage Examples

Private Properties

Property Type Description
CommunicationThread void
DiscardIncomingData void
Reconnect void

Public Methods

Method Description
Connect ( string ip, int port ) : void

Connect to SRV-1 Blackfin robot/camera.

The method establishes connection to SRV-1 Blackfin robot/camera. If it succeeds then other methods can be used to manipulate the robot.

The method calls Disconnect before making any connection attempts to make sure previous connection is closed.

ControlMotors ( MotorCommand command ) : void

Control SRV-1 robot's motors using predefined commands.

Controlling SRV-1 motors with this method is only available after at least one direct motor command is sent, which is done using StopMotors or RunMotors methods.

ControlServos ( int leftServo, int rightServo ) : void

Direct servos control of SRV-1 robot.

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.

The method sends 'sab' SRV-1 command (see SRV-1 Control Protocol), which controls 2nd bank of servos using 6th and 7th timers.

DisableFailsafeMode ( ) : void

Disable fail safe mode.

The method disable fail safe mode, which was set using EnableFailsafeMode method.

Disconnect ( ) : void

Disconnect from SRV-1 Blackfin robot.

The method disconnects from SRV-1 robot making all other methods unavailable (except Connect method). In the case if user obtained instance of camera using GetCamera method, the video will be stopped automatically (and those SRV1Camera instances should be discarded).

EnableFailsafeMode ( int leftSpeed, int rightSpeed ) : void

Enables fail safe mode - setting motors' speed after timeout.

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).

FlipVideo ( bool isFlipped ) : void

Flip video capture or not (for use with upside-down camera).

GetCamera ( ) : SRV1Camera

Get camera object for the SRV-1 Blackfin robot/camera.

The method provides an instance of 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 GetImage method can be used.

Sample usage:

// 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 }
GetImage ( ) : Bitmap

Get single image from the SRV-1 Blackfin camera.

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 GetCamera method is more preferred for continuous video frames.

GetRunningTime ( ) : long

Get SRV-1 running time.

GetVersion ( ) : string

Get SRV-1 firmware version string.

I2CReadByte ( byte deviceID, byte register ) : byte

Read byte from I2C device.

I2CReadWord ( byte deviceID, byte register ) : ushort

Read word from I2C device.

I2CWriteByte ( byte deviceID, byte register, byte byteToWrite ) : void

Write byte to I2C device.

I2CWriteWord ( byte deviceID, byte register, byte firstByteToWrite, byte secondByteToWrite ) : void

Write two bytes to I2C device.

RunMotors ( int leftSpeed, int rightSpeed, int duration ) : void

Run motors connected to SRV-1 robot.

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).

The method sends 'Mabc' SRV-1 command (see SRV-1 Control Protocol), which uses 2nd and 3rd timers for controlling motors/servos.

SRV1 ( ) : System

Initializes a new instance of the SRV1 class.

Send ( byte request ) : void

Enqueue communication request.

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.

The method is useful for those SRV-1 commands, which does not assume any response data in the command's reply.

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.

SendAndReceive ( byte request, byte responseBuffer ) : int

Enqueue communication request and wait for reply.

The method puts specified command into communication queue and waits until the command is sent to SRV-1 Blackfin robot and reply is received.

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 IndexOutOfRangeException exception is thrown. It is user's responsibility to provide response buffer of the correct size. Check definition of the SRV-1 Control Protocol for information about supported commands and responses.

SetQuality ( int quality ) : void

Set video quality.

The method sets video quality, which is specified in [1, 8] range - 1 is the highest quality level, 8 is the lowest quality level.

Setting higher quality level 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.

SetResolution ( VideoResolution resolution ) : void

Set video resolution.

Setting higher quality level 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.

StopMotors ( ) : void

Stop both motors.

The method stops both motors connected to the SRV-1 robot by calling RunMotors method specifying 0 for motors' speed.

UltrasonicPing ( ) : float[]

Ping ultrasonic ranging modules.

The method sends 'p' SRV-1 command (see SRV-1 Control Protocol), 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.

Private Methods

Method Description
CommunicationThread ( ) : void
DiscardIncomingData ( Socket socket, ManualResetEvent stopEvent ) : void
Reconnect ( ) : void

Method Details

Connect() public method

Connect to SRV-1 Blackfin robot/camera.

The method establishes connection to SRV-1 Blackfin robot/camera. If it succeeds then other methods can be used to manipulate the robot.

The method calls Disconnect before making any connection attempts to make sure previous connection is closed.

Failed connecting to SRV-1.
public Connect ( string ip, int port ) : void
ip string IP address of SRV-1 robot.
port int Port number to connect to.
return void

ControlMotors() public method

Control SRV-1 robot's motors using predefined commands.

Controlling SRV-1 motors with this method is only available after at least one direct motor command is sent, which is done using StopMotors or RunMotors methods.

public ControlMotors ( MotorCommand command ) : void
command MotorCommand Motor command to send to the SRV-1 Blackfin robot.
return void

ControlServos() public method

Direct servos control of SRV-1 robot.

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.

The method sends 'sab' SRV-1 command (see SRV-1 Control Protocol), which controls 2nd bank of servos using 6th and 7th timers.

public ControlServos ( int leftServo, int rightServo ) : void
leftServo int Left servo setting, [0, 100].
rightServo int Right servo setting, [0, 100].
return void

DisableFailsafeMode() public method

Disable fail safe mode.

The method disable fail safe mode, which was set using EnableFailsafeMode method.

public DisableFailsafeMode ( ) : void
return void

Disconnect() public method

Disconnect from SRV-1 Blackfin robot.

The method disconnects from SRV-1 robot making all other methods unavailable (except Connect method). In the case if user obtained instance of camera using GetCamera method, the video will be stopped automatically (and those SRV1Camera instances should be discarded).

public Disconnect ( ) : void
return void

EnableFailsafeMode() public method

Enables fail safe mode - setting motors' speed after timeout.

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).

public EnableFailsafeMode ( int leftSpeed, int rightSpeed ) : void
leftSpeed int Left motor's speed, [-127, 127].
rightSpeed int Right motor's speed, [-127, 127].
return void

FlipVideo() public method

Flip video capture or not (for use with upside-down camera).
public FlipVideo ( bool isFlipped ) : void
isFlipped bool Specifies if video should be flipped (), /// or not ().
return void

GetCamera() public method

Get camera object for the SRV-1 Blackfin robot/camera.

The method provides an instance of 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 GetImage method can be used.

Sample usage:

// 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 }
Not connected to SRV-1. Connect to SRV-1 robot/camera /// before using this method.
public GetCamera ( ) : SRV1Camera
return SRV1Camera

GetImage() public method

Get single image from the SRV-1 Blackfin camera.

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 GetCamera method is more preferred for continuous video frames.

Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect.
public GetImage ( ) : Bitmap
return System.Drawing.Bitmap

GetRunningTime() public method

Get SRV-1 running time.
Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect. Failed parsing response from SRV-1.
public GetRunningTime ( ) : long
return long

GetVersion() public method

Get SRV-1 firmware version string.
Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect.
public GetVersion ( ) : string
return string

I2CReadByte() public method

Read byte from I2C device.
Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect. Failed parsing response from SRV-1.
public I2CReadByte ( byte deviceID, byte register ) : byte
deviceID byte I2C device ID (7 bit notation).
register byte I2C device register to read.
return byte

I2CReadWord() public method

Read word from I2C device.
Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect. Failed parsing response from SRV-1.
public I2CReadWord ( byte deviceID, byte register ) : ushort
deviceID byte I2C device ID (7 bit notation).
register byte I2C device register to read.
return ushort

I2CWriteByte() public method

Write byte to I2C device.
Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect.
public I2CWriteByte ( byte deviceID, byte register, byte byteToWrite ) : void
deviceID byte I2C device ID (7 bit notation).
register byte I2C device register to write to.
byteToWrite byte Byte to write to the specified register of the specified device.
return void

I2CWriteWord() public method

Write two bytes to I2C device.
Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect.
public I2CWriteWord ( byte deviceID, byte register, byte firstByteToWrite, byte secondByteToWrite ) : void
deviceID byte I2C device ID (7 bit notation).
register byte I2C device register to write to.
firstByteToWrite byte First byte to write to the specified register of the specified device.
secondByteToWrite byte Second byte to write to the specified register of the specified device.
return void

RunMotors() public method

Run motors connected to SRV-1 robot.

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).

The method sends 'Mabc' SRV-1 command (see SRV-1 Control Protocol), which uses 2nd and 3rd timers for controlling motors/servos.

public RunMotors ( int leftSpeed, int rightSpeed, int duration ) : void
leftSpeed int Left motor's speed, [-127, 127].
rightSpeed int Right motor's speed, [-127, 127].
duration int Time duration to run motors measured in number /// of 10 milliseconds (0 for infinity), [0, 255].
return void

SRV1() public method

Initializes a new instance of the SRV1 class.
public SRV1 ( ) : System
return System

Send() public method

Enqueue communication request.

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.

The method is useful for those SRV-1 commands, which does not assume any response data in the command's reply.

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.

public Send ( byte request ) : void
request byte Array of bytes (command) to send to SRV-1 Blackfin robot/camera.
return void

SendAndReceive() public method

Enqueue communication request and wait for reply.

The method puts specified command into communication queue and waits until the command is sent to SRV-1 Blackfin robot and reply is received.

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 IndexOutOfRangeException exception is thrown. It is user's responsibility to provide response buffer of the correct size. Check definition of the SRV-1 Control Protocol for information about supported commands and responses.

Not connected to SRV-1. Connection lost or communicaton failure. Response buffer is too small.
public SendAndReceive ( byte request, byte responseBuffer ) : int
request byte Array of bytes (command) to send to SRV-1 Blackfin robot/camera.
responseBuffer byte Buffer to read response into.
return int

SetQuality() public method

Set video quality.

The method sets video quality, which is specified in [1, 8] range - 1 is the highest quality level, 8 is the lowest quality level.

Setting higher quality level 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.

Invalid quality level was specified.
public SetQuality ( int quality ) : void
quality int Video quality to set, [1, 8].
return void

SetResolution() public method

Set video resolution.

Setting higher quality level 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.

Invalid resolution was specified.
public SetResolution ( VideoResolution resolution ) : void
resolution VideoResolution Video resolution to set.
return void

StopMotors() public method

Stop both motors.

The method stops both motors connected to the SRV-1 robot by calling RunMotors method specifying 0 for motors' speed.

public StopMotors ( ) : void
return void

UltrasonicPing() public method

Ping ultrasonic ranging modules.

The method sends 'p' SRV-1 command (see SRV-1 Control Protocol), 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.

Not connected to SRV-1. Connect to SRV-1 before using /// this method. Connection lost or communicaton failure. Try to reconnect. Failed parsing response from SRV-1.
public UltrasonicPing ( ) : float[]
return float[]