mirror of
https://github.com/OSURoboticsClub/Rover_2017_2018.git
synced 2025-11-09 02:31:14 +00:00
Added non-final versions of iris and tower firmware.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/**
|
||||
* Modbus slave example 3:
|
||||
* The purpose of this example is to link a data array
|
||||
* from the Arduino to an external device through RS485.
|
||||
*
|
||||
* Recommended Modbus Master: QModbus
|
||||
* http://qmodbus.sourceforge.net/
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// assign the Arduino pin that must be connected to RE-DE RS485 transceiver
|
||||
#define TXEN 4
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16] = {
|
||||
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, 65535 };
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* u8serno : serial port (use 0 for Serial)
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus slave(1,0,TXEN); // this is slave @1 and RS-485
|
||||
|
||||
void setup() {
|
||||
slave.begin( 19200 ); // baud-rate at 19200
|
||||
}
|
||||
|
||||
void loop() {
|
||||
slave.poll( au16data, 16 );
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
/**
|
||||
* Modbus master example 2:
|
||||
* The purpose of this example is to query several sets of data
|
||||
* from an external Modbus slave device.
|
||||
* The link media can be USB or RS232.
|
||||
*
|
||||
* Recommended Modbus slave:
|
||||
* diagslave http://www.modbusdriver.com/diagslave.html
|
||||
*
|
||||
* In a Linux box, run
|
||||
* "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
|
||||
* This is:
|
||||
* serial port /dev/ttyUSB0 at 19200 baud 8N1
|
||||
* RTU mode and address @1
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
uint16_t au16data[16]; //!< data array for modbus network sharing
|
||||
uint8_t u8state; //!< machine state
|
||||
uint8_t u8query; //!< pointer to message query
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* u8serno : serial port (use 0 for Serial)
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
|
||||
|
||||
/**
|
||||
* This is an structe which contains a query to an slave device
|
||||
*/
|
||||
modbus_t telegram[2];
|
||||
|
||||
unsigned long u32wait;
|
||||
|
||||
void setup() {
|
||||
// telegram 0: read registers
|
||||
telegram[0].u8id = 1; // slave address
|
||||
telegram[0].u8fct = 3; // function code (this one is registers read)
|
||||
telegram[0].u16RegAdd = 0; // start address in slave
|
||||
telegram[0].u16CoilsNo = 4; // number of elements (coils or registers) to read
|
||||
telegram[0].au16reg = au16data; // pointer to a memory array in the Arduino
|
||||
|
||||
// telegram 1: write a single register
|
||||
telegram[1].u8id = 1; // slave address
|
||||
telegram[1].u8fct = 6; // function code (this one is write a single register)
|
||||
telegram[1].u16RegAdd = 4; // start address in slave
|
||||
telegram[1].u16CoilsNo = 1; // number of elements (coils or registers) to read
|
||||
telegram[1].au16reg = au16data+4; // pointer to a memory array in the Arduino
|
||||
|
||||
master.begin( 19200 ); // baud-rate at 19200
|
||||
master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
|
||||
u32wait = millis() + 1000;
|
||||
u8state = u8query = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch( u8state ) {
|
||||
case 0:
|
||||
if (millis() > u32wait) u8state++; // wait state
|
||||
break;
|
||||
case 1:
|
||||
master.query( telegram[u8query] ); // send query (only once)
|
||||
u8state++;
|
||||
u8query++;
|
||||
if (u8query > 2) u8query = 0;
|
||||
break;
|
||||
case 2:
|
||||
master.poll(); // check incoming messages
|
||||
if (master.getState() == COM_IDLE) {
|
||||
u8state = 0;
|
||||
u32wait = millis() + 1000;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
au16data[4] = analogRead( 0 );
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,119 @@
|
||||
/**
|
||||
* Modbus slave example 2:
|
||||
* The purpose of this example is to link the Arduino digital and analog
|
||||
* pins to an external device.
|
||||
*
|
||||
* Recommended Modbus Master: QModbus
|
||||
* http://qmodbus.sourceforge.net/
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
#define ID 1
|
||||
|
||||
Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
|
||||
boolean led;
|
||||
int8_t state = 0;
|
||||
unsigned long tempus;
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[9];
|
||||
|
||||
/**
|
||||
* Setup procedure
|
||||
*/
|
||||
void setup() {
|
||||
io_setup(); // I/O settings
|
||||
|
||||
// start communication
|
||||
slave.begin( 19200 );
|
||||
tempus = millis() + 100;
|
||||
digitalWrite(13, HIGH );
|
||||
}
|
||||
|
||||
/**
|
||||
* Loop procedure
|
||||
*/
|
||||
void loop() {
|
||||
// poll messages
|
||||
// blink led pin on each valid message
|
||||
state = slave.poll( au16data, 9 );
|
||||
|
||||
if (state > 4) {
|
||||
tempus = millis() + 50;
|
||||
digitalWrite(13, HIGH);
|
||||
}
|
||||
if (millis() > tempus) digitalWrite(13, LOW );
|
||||
|
||||
// link the Arduino pins to the Modbus array
|
||||
io_poll();
|
||||
}
|
||||
|
||||
/**
|
||||
* pin maping:
|
||||
* 2 - digital input
|
||||
* 3 - digital input
|
||||
* 4 - digital input
|
||||
* 5 - digital input
|
||||
* 6 - digital output
|
||||
* 7 - digital output
|
||||
* 8 - digital output
|
||||
* 9 - digital output
|
||||
* 10 - analog output
|
||||
* 11 - analog output
|
||||
* 14 - analog input
|
||||
* 15 - analog input
|
||||
*
|
||||
* pin 13 is reserved to show a successful query
|
||||
*/
|
||||
void io_setup() {
|
||||
// define i/o
|
||||
pinMode(2, INPUT);
|
||||
pinMode(3, INPUT);
|
||||
pinMode(4, INPUT);
|
||||
pinMode(5, INPUT);
|
||||
pinMode(6, OUTPUT);
|
||||
pinMode(7, OUTPUT);
|
||||
pinMode(8, OUTPUT);
|
||||
pinMode(9, OUTPUT);
|
||||
pinMode(10, OUTPUT);
|
||||
pinMode(11, OUTPUT);
|
||||
pinMode(13, OUTPUT);
|
||||
|
||||
digitalWrite(6, LOW );
|
||||
digitalWrite(7, LOW );
|
||||
digitalWrite(8, LOW );
|
||||
digitalWrite(9, LOW );
|
||||
digitalWrite(13, HIGH ); // this is for the UNO led pin
|
||||
analogWrite(10, 0 );
|
||||
analogWrite(11, 0 );
|
||||
}
|
||||
|
||||
/**
|
||||
* Link between the Arduino pins and the Modbus array
|
||||
*/
|
||||
void io_poll() {
|
||||
// get digital inputs -> au16data[0]
|
||||
bitWrite( au16data[0], 0, digitalRead( 2 ));
|
||||
bitWrite( au16data[0], 1, digitalRead( 3 ));
|
||||
bitWrite( au16data[0], 2, digitalRead( 4 ));
|
||||
bitWrite( au16data[0], 3, digitalRead( 5 ));
|
||||
|
||||
// set digital outputs -> au16data[1]
|
||||
digitalWrite( 6, bitRead( au16data[1], 0 ));
|
||||
digitalWrite( 7, bitRead( au16data[1], 1 ));
|
||||
digitalWrite( 8, bitRead( au16data[1], 2 ));
|
||||
digitalWrite( 9, bitRead( au16data[1], 3 ));
|
||||
|
||||
// set analog outputs
|
||||
analogWrite( 10, au16data[2] );
|
||||
analogWrite( 11, au16data[3] );
|
||||
|
||||
// read analog inputs
|
||||
au16data[4] = analogRead( 0 );
|
||||
au16data[5] = analogRead( 1 );
|
||||
|
||||
// diagnose communication
|
||||
au16data[6] = slave.getInCnt();
|
||||
au16data[7] = slave.getOutCnt();
|
||||
au16data[8] = slave.getErrCnt();
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Modbus master example 1:
|
||||
* The purpose of this example is to query an array of data
|
||||
* from an external Modbus slave device.
|
||||
* The link media can be USB or RS232.
|
||||
*
|
||||
* Recommended Modbus slave:
|
||||
* diagslave http://www.modbusdriver.com/diagslave.html
|
||||
*
|
||||
* In a Linux box, run
|
||||
* "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
|
||||
* This is:
|
||||
* serial port /dev/ttyUSB0 at 19200 baud 8N1
|
||||
* RTU mode and address @1
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16];
|
||||
uint8_t u8state;
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* u8serno : serial port (use 0 for Serial)
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus master(0,0,0); // this is master and RS-232 or USB-FTDI
|
||||
|
||||
/**
|
||||
* This is an structe which contains a query to an slave device
|
||||
*/
|
||||
modbus_t telegram;
|
||||
|
||||
unsigned long u32wait;
|
||||
|
||||
void setup() {
|
||||
master.begin( 19200 ); // baud-rate at 19200
|
||||
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
|
||||
u32wait = millis() + 1000;
|
||||
u8state = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch( u8state ) {
|
||||
case 0:
|
||||
if (millis() > u32wait) u8state++; // wait state
|
||||
break;
|
||||
case 1:
|
||||
telegram.u8id = 1; // slave address
|
||||
telegram.u8fct = 3; // function code (this one is registers read)
|
||||
telegram.u16RegAdd = 1; // start address in slave
|
||||
telegram.u16CoilsNo = 4; // number of elements (coils or registers) to read
|
||||
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
|
||||
|
||||
master.query( telegram ); // send query (only once)
|
||||
u8state++;
|
||||
break;
|
||||
case 2:
|
||||
master.poll(); // check incoming messages
|
||||
if (master.getState() == COM_IDLE) {
|
||||
u8state = 0;
|
||||
u32wait = millis() + 100;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
/**
|
||||
* Modbus slave example 1:
|
||||
* The purpose of this example is to link a data array
|
||||
* from the Arduino to an external device.
|
||||
*
|
||||
* Recommended Modbus Master: QModbus
|
||||
* http://qmodbus.sourceforge.net/
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16] = {
|
||||
3, 1415, 9265, 4, 2, 7182, 28182, 8, 0, 0, 0, 0, 0, 0, 1, 65535 };
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* u8serno : serial port (use 0 for Serial)
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus slave(1,0,0); // this is slave @1 and RS-232 or USB-FTDI
|
||||
|
||||
void setup() {
|
||||
slave.begin( 19200 ); // baud-rate at 19200
|
||||
//slave.begin( 19200, SERIAL_8E1 ); // 19200 baud, 8-bits, even, 1-bit stop
|
||||
}
|
||||
|
||||
void loop() {
|
||||
slave.poll( au16data, 16 );
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Modbus master example 2:
|
||||
* The purpose of this example is to query an array of data
|
||||
* from an external Modbus slave device.
|
||||
* This example is similar to "simple_master", but this example
|
||||
* allows you to use software serial instead of hardware serial
|
||||
* in case that you want to use D1 & D2 for other purposes.
|
||||
* The link media can be USB or RS232.
|
||||
|
||||
The circuit:
|
||||
* software serial rx(D3) connect to tx pin of another device
|
||||
* software serial tx(D4) connect to rx pin of another device
|
||||
|
||||
* In this example, we will use two important methods so that we can use
|
||||
* software serial.
|
||||
*
|
||||
* 1. Modbus::Modbus(uint8_t u8id)
|
||||
* This is a constructor for a Master/Slave through USB/RS232C via software serial
|
||||
* This constructor only specifies u8id (node address) and should be only
|
||||
* used if you want to use software serial instead of hardware serial.
|
||||
* This method is called if you create a ModBus object with only on parameter "u8id"
|
||||
* u8id is the node address of the arduino that will be programmed on,
|
||||
* 0 for master and 1..247 for slave
|
||||
* for example: Modbus master(0);
|
||||
* If you use this constructor you have to begin ModBus object by
|
||||
* using "void Modbus::begin(SoftwareSerial *softPort, long u32speed)".
|
||||
*
|
||||
* 2. void Modbus::begin(SoftwareSerial *sPort, long u32speed)
|
||||
* Initialize class object.
|
||||
* This is the method you have to use if you construct the ModBus object by using
|
||||
* Modbus::Modbus(uint8_t u8id) in order to use software serial and to avoid problems.
|
||||
* You have to create a SoftwareSerial object on your own, as shown in the example.
|
||||
* sPort is a pointer to your SoftwareSerial object, u32speed is the baud rate, in
|
||||
* standard increments (300..115200)
|
||||
|
||||
created long time ago
|
||||
by smarmengol
|
||||
modified 29 July 2016
|
||||
by Helium6072
|
||||
|
||||
This example code is in the public domain.
|
||||
*/
|
||||
|
||||
#include <ModbusRtu.h>
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
// data array for modbus network sharing
|
||||
uint16_t au16data[16];
|
||||
uint8_t u8state;
|
||||
|
||||
/**
|
||||
* Modbus object declaration
|
||||
* u8id : node id = 0 for master, = 1..247 for slave
|
||||
* u8serno : serial port (use 0 for Serial)
|
||||
* u8txenpin : 0 for RS-232 and USB-FTDI
|
||||
* or any pin number > 1 for RS-485
|
||||
*/
|
||||
Modbus master(0); // this is master and RS-232 or USB-FTDI via software serial
|
||||
|
||||
/**
|
||||
* This is an structe which contains a query to an slave device
|
||||
*/
|
||||
modbus_t telegram;
|
||||
|
||||
unsigned long u32wait;
|
||||
|
||||
SoftwareSerial mySerial(3, 5);//Create a SoftwareSerial object so that we can use software serial. Search "software serial" on Arduino.cc to find out more details.
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);//use the hardware serial if you want to connect to your computer via usb cable, etc.
|
||||
master.begin( &mySerial, 9600 ); // begin the ModBus object. The first parameter is the address of your SoftwareSerial address. Do not forget the "&". 9600 means baud-rate at 9600
|
||||
master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
|
||||
u32wait = millis() + 1000;
|
||||
u8state = 0;
|
||||
}
|
||||
|
||||
void loop() {
|
||||
switch( u8state ) {
|
||||
case 0:
|
||||
if (millis() > u32wait) u8state++; // wait state
|
||||
break;
|
||||
case 1:
|
||||
telegram.u8id = 104; // slave address
|
||||
telegram.u8fct = 4; // function code (this one is registers read)
|
||||
telegram.u16RegAdd = 3; // start address in slave
|
||||
telegram.u16CoilsNo = 1; // number of elements (coils or registers) to read
|
||||
telegram.au16reg = au16data; // pointer to a memory array in the Arduino
|
||||
|
||||
master.query( telegram ); // send query (only once)
|
||||
u8state++;
|
||||
break;
|
||||
case 2:
|
||||
master.poll(); // check incoming messages
|
||||
if (master.getState() == COM_IDLE) {
|
||||
u8state = 0;
|
||||
u32wait = millis() + 2000;
|
||||
Serial.println(au16data[0]);//Or do something else!
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user