Project Overview

aliazzz
There is a newer version of this page. You can find it here.

ERROR! The markdown supplied could not be parsed correctly. Did you forget to surround a code snippet with "~~~~"?

[[members limit=20]]

[TOC]
# The Monarco HAT

The Monarco HAT is a robust industrial graded HAT, perfectly suited for IOT projects, small home-automation or industrial projects and much more ... 
It protects your Raspberry Pi from overvoltage or short-circuiting and simultaneously provides you with enough IO channels and channel configuration versatility.

A CODESYS V3 library was missing, so I wrote one to fill the gap after studying the C & Java Node-JS code examples and documentation provided by Monarco.
Though tested, it will probably still contain bugs =( If you spot a bug, share it so we can fix it.

Implemented as an IO device-driver, so no function block calls to the HAT in your software necessary. 
The HAT is exposed as a hardware device with IO channels and parameters.
Just write code, attach variable to a I/O channel in the hardware tree, ready!

* 100% Open source,
* 100% Pure IEC 61131-3 code (ST)


-----

##Currently implemented parameters

* 4 DI channels,
* 4 DO channels,
* 2 AI channels (12bit),
* 2 AOchannels (12bit),
* Hardware Watchdog,
* Control Byte,
* Stable, but work in progress ..


-----


##Limitations

All functionality of the HAT is allready implemented into the core of the driver but some functionality still needs to be routed as parameters

Not yet routed to parameters are;

* RS485 configuration,
* DO channels PWM configuration,
* DI channels Counter configuration,
* 1-Wire bus.


# Changelog  

v2.0.0.2 
    devdescr.xml: vendor id set to 0004, device id set to 0005, see https://forge.codesys.com/drv/io-drivers/database/Home/ for details.     
~~~
    <DeviceIdentification>
      <Type>501</Type>
      <Id>0004 0005</Id>
      <Version>2.0.0.2</Version>
    </DeviceIdentification>
~~~

v2.0.0.1 info
Initial release in cforge, forked from my git : https://github.com/Aliazzzz/Monarco-HAT-library-for-CODESYS-V3


-----


# Hardware prerequisitories

1.     A Raspberry Pi, see https://www.raspberrypi.org/
2.     A Monarco HAT, see https://www.monarco.io/
3.     8 GB SD Card ...


## Hardware installation and setup

Attach Monarco HAT to Raspberry Pi and power it up;

~~~
sudo cat /proc/device-tree/hat/vendor
~~~

> should return "REX Controls".

~~~
sudo cat /proc/device-tree/hat/product
~~~

> should return "Monarco HAT".

Disable system console on UART

~~~
sudo sed 's/ console=serial0,[0-9]\+//' -i /boot/cmdline.txt
sudo reboot
~~~

Install essential tools

~~~
sudo apt update
sudo apt install git
~~~

Flash Monarco HAT EEPROM (to avoid manual installation of overlay)

~~~
sudo git clone https://github.com/monarco/monarco-hat-firmware-bin

cd monarco-hat-firmware-bin
./monarco-eeprom.sh update
~~~


# Software prerequisitories

1.     CODESYS V3 IDE available at https://store.codesys.com/codesys.html,
2.     CODESYS Raspberry Pi .package available at https://store.codesys.com/codesys-control-for-raspberry-pi-sl.html,
3.     CODESYS Raspberry Pi SL Demo or License (The Demo is unrestricted in technical capabilities but will only run for two hours straight, after which it stops and you have to set it in run again yourself by logging in)
4.     Contents of SVN directory : [SVN](/drv/spi-monarco/svn)



##Codesys Runtime installation

Installing CODESYS runtime component on pi (Demo or licensed), just follow CODESYS online help steps, it's easy!
https://help.codesys.com/webapp/_rbp_install_runtime;product=CODESYS_Control_for_Raspberry_Pi_SL;version=3.5.12.0

This video palso provides a good oerview on how to prepare your pi for CODESYS.
[[embed url=http://www.youtube.com/https://www.youtube.com/watch?v=6FPf3RHWyeU]]



##Monarco package installation

Install the Monarco codesys package via;

* Double-click the package or via CODESYS IDE Package Manager,
* Install the loose components via the Library / Device Repository, found under Tools menu option in CODESYS IDE.


##Monarco UART configuration

Enable CODESYS to use the RS485 UART on the Monarco Hat.

Switch to etc direcory and edit the CODESYSControl.cfg;

~~~
cd etc/
sudo nano CODESYSControl.cfg
~~~

Add the following lines;

~~~
[SysCom] Linux.Devicefile=/dev/ttyAMA
~~~

Now save and Quit nano.


Now, do a forced NTP sync

~~~
sudo timedatectl
~~~

This will force to sync time with some time server and returns something like, depending on date/time and your time-zone;

     Local time: zo 2018-09-23 14:46:17 CEST
     Universal time: zo 2018-09-23 12:46:17 UTC
     RTC time: zo 2018-09-23 12:46:18
     Time zone: Europe/Amsterdam (CEST, +0200)
     Network time on: yes
     NTP synchronized: yes
     RTC in local TZ: no


Now you can use the HAT, RS-485 and the Real-Time Clock from within a CODESYS IEC application. 
Access the RS485 UART via a comlib of you own flavour in CODESYS (like CAA SerialCOM library).


##Running the CODESYS project

Open the provided example project file.

Check/Set SPI master parameters:

~~~
Mode 0,
SPI bits 8,
Speed(Hz) 1000000 (=1MHz) => can be set up to 4 MHz, slower speeds avoid chance on crc errors
~~~

Compile, download and run and enjoy!


[[members limit=20]]