CIP Statemachine - Arduino

Table of Contents

logo-large

Tutorial Actifsource Tutorial – CIP Statemachine - Arduino
Required Time - 120 Minutes
Prerequisites - Actifsource Tutorial – Installing Actifsource
- Actifsource Tutorial – Simple Service
- Actifsource Tutorial – CIP Statemachine - Lamp
Goal - Compiling and running the CIP Statemachine on the Arduino UNO
Topics covered - Arduino SDK
- Simple Arduino project
- CIP Arduino project
Notation ↪ To do
ⓘ Information
Bold: Terms from actifsource or other technologies and tools
Bold underlined: actifsource Resources
Monospaced: User input
Italics: Important terms in current situation
Disclaimer The authors do not accept any liability arising out of the application or use of any information or equipment described herein. The information contained within this document is by its very nature incomplete. Therefore the authors accept no responsibility for the precise accuracy of the documentation contained herein. It should be used rather as a guide and starting point.
Contact Actifsource AG
Täfernstrasse 37
5405 Baden-Dättwil
Switzerland
www.actifsource.com
Trademark Actifsource is a registered trademark of Actifsource AG in Switzerland, the EU, USA, and China. other names appearing on the site may be trademarks of their respective owners.

Overview

  • Arduino SDK

    • The Arduino SDK contains all the necessary source code for your Arduino
    • Installing the com port driver for your Arduino
  • AVR Eclipse Plugin

    • The AVR Plugin is an extension to the C/C++ Development Toolkit to support development for the Atmel AVR series of embedded processors.
    • Configure the AVR Plugin for your Arduino board
  • Simple Arduino project

    • Setup a new C/C++ project
    • Write a very simple code which switches an LED on and off
    • Compile the project including the Arduino core
    • Download to the Arduino target and run
  • CIP Arduino project

    • Modeling a reactive state machine using the CIP method
    • Generating C code for the state machine
    • Connecting the generated state machine code to the Arduino I/O
    • Downloading and testing to the Arduino board

Part I Installing Eclipse & Arduino

  • Install the Eclipse IDE for C/C++ Developers (C/C++ Development Tool) www.eclipse.org/downloads
  • Install the Actifsource Enterprise plugin
  • The Arduino SDK contains all the necessary source code for your Arduino
  • Installing the com port driver for your Arduino

Arduino Software Setup

image2 image2

  • Download your Arduino SDK from www.arduino.cc

  • Unzip the Arduino SDK on your hard disk

    • We use C:\arduino-1.0.3\ in this example

image3 image3

  • Plugin your Arduino to a USB port

    • The installation will fail
  • Open the Device Manager from the Control Panel

  • Find the failed device

  • Install new driver

  • Search the driver on your computer

  • Specify C:\arduino-1.0.3\drivers as the place to look for the driver

    • Warning: You must not select C:\arduino-1.0.3\drivers\FTDI USB Drivers

image4 image4

  • Check if the driver has been installed correctly

  • Important: Remember the com port

    • COM3 in this example

image5 image5

  • Open the boards.txt file in C:\arduino-1.0.3\hardware\arduino

  • Print the information for your hardware platform

    • Arduino Uno in this example
  • For Linux you can simply install gcc-avr avr-libc avrdude:

sudo apt-get install gcc-avr avr-libc avrdude

AVR Eclipse Plugin

  • The AVR Plugin (for this tutorial version 2.4.1 will be used)for Eclipse is an extension to the C/C++ Development Toolkit to support development for the Atmel AVR series of embedded processors.
  • Configure the AVR Plugin for your Arduino board

image6 image6

  • Start your Eclipse with a new workspace

    • workspace_arduino in this example
  • In your Eclipse, click Help/Install new Software…

  • Add the AVR-Eclipse plugin update site avr-eclipse.sourceforge.net

  • Installing the plugin requires eclipse to restart

image7 image7

  • Configure the AVR Eclipse Plugin under Windows/Preferences/AVR/Paths
  • Set AVR-GCC to custom value C:\arduino-1.0.3\hardware\tools\avr\bin or /usr/bin
  • Set GNU make to custom value C:\arduino-1.0.3\hardware\tools\avr\utils\\bin or /usr/bin
  • Set SVR-GCC to custom value C:\arduino-1.0.3\hardware\tools\avr\avr\\include or /usr/lib/avr/include
  • Set SVR-GCC to custom value C:\arduino-1.0.3\hardware\tools\avr\bin or /usr/bin
  • Important: Press Apply

Part II Simple Arduino project

  • Setup a new C/C++ project
  • Write a very simple code which switches an LED on and off
  • Compile the project including the Arduino core
  • Download to the Arduino target and run

Simple Arduino project

image11 image11

  • Create a new C++ Project

  • You have to create a C++ project even if you plan to write C code since we have to compile the Arduino Core

  • If you can’t create a C/C++ Project you have probably not downloaded the Eclipse for C/C++ Developers

image12 image12

  • Create a new C++ project named ch.actifsource.tutorial.cip.arduino
  • Choose AVR Cross Target Application
  • Choose the AVR-GCC Toolchain
  • Click Next

image13 image13

  • Do not create a Debug Version

  • The debug code is too large to fit on the Arduino Uno

  • See boards.txt for the maximum code size

    • uno.upload.maximum_size=32256 for this example
  • Click Fish

  • Open the C/C++ Perspective if you are asked or open it manually in the upper right corner image15 image15

Arduino Build Variables Arduino Build Variables

  • For the newly created ch.actifsource.tutorial.cip.arduino, choose Properties (Mouse-Click-Right on the project)
  • Select C/C++ Build
  • In the Build Variables Tab, enter the following variables:
    • AVRDUDEOPTIONS, set to -p atmega328p -c arduino -P/dev/ttyUSB0 -b 115200
    • AVRTARGETFCPU, set to 16000000
    • AVRTARGETMCU, set to atmega328p
  • Click Apply

Arduino Environment Variables Arduino Environment Variables

  • In the Environment Tab, enter the following variables:
    • AVRTARGETFCPU, set to 16000000
    • AVRTARGETMCU, set to atmega328p
  • Click Apply

Ardunio Settings Ardunio Settings

  • In the Settings Tab, set AVRDude to true. This will automatically download the program to the attached board
  • Click Apply

image18 image18

  • This step is only needed on Windows

  • SimpleProject/Properties/C/C++Build/Settings/AVR Compiler/Directories

  • Set Include Path C:\arduino-1.0.3\hardware\tools\avr\avr\include

  • Set Include Path C:\arduino-1.0.3\hardware\arduino\cores\arduino

  • Set Include Path C:\arduino-1.0.3\hardware\arduino\variants\standard

    • See boards.txt (uno.build.variant) for your variant (standard for this example)
  • Click OK

image19 image19

  • Do the same again for SimpleProject/Properties/C/C++Build/Settings/AVR C++ Compiler/Directories

  • Set Include Path C:\arduino-1.0.3\hardware\tools\avr\avr\include

  • Set Include Path C:\arduino-1.0.3\hardware\arduino\cores\arduino

  • Set Include Path C:\arduino-1.0.3\hardware\arduino\variants\standard

    • See boards.txt (uno.build.variant) for your variant (standard for this example)
  • Click OK

image20 image20

  • Create a new folder src in the project

image23 image23

  • Create a new file main.c in the folder src
#include <avr/io.h>
#include <util/delay.h>

#define DELAYTIME 500  // 500ms Verzögerung

int main(void)
{
  DDRB |= _BV(DDB5); // Setze Pin PB5 (Arduino LED) als Ausgang

  while (1) {
    PORTB |= _BV(PORTB5);  // LED einschalten
    _delay_ms(DELAYTIME);  // Wartezeit

    PORTB &= ~_BV(PORTB5); // LED ausschalten
    _delay_ms(DELAYTIME);  // Wartezeit
  }

  return 0; // Dieser Punkt wird nie erreicht
}
  • Write a very simple Arduino program in the file main.c as shown above
  • If you enabled Build on resource save (Auto build) in Project/Properties/C/C++Build/Behaviour before, the code should now be built
  • You can also build manually by pressing Project/Build All (Ctrl+B ) or the build button in the Eclipse toolbar image24 image24
Info

Your code is probably not linking if you named your file main.cpp instead if main.c

image25 image25

  • Download your code to the Arduino

image27 image27

  • Mount your LED between Pin 13 and GND

    • Don’t forget the 330Ω resistor for a 1.7V LED with 10mA 🡪 (5V-1.7V)/0.01A = 330Ω
  • The program starts automatically after the download has been completed

    • The LED should switch on and off once per second

Part III CIP Arduino project

  • Modeling a reactive state machine using the CIP method
  • Generating C code for the state machine
  • Connecting the generated state machine code to the Arduino I/O
  • Downloading and testing to the Arduino board

CIP Arduino project

image28 image28

  • Switch to the Actifsource Perspective image29 image29
  • Select your project ch.actifsource.tutorial.cip.arduino in the Project Explorer
  • Select Configure/Add Actifsource Nature

image30 image30

  • Open Properties/actifsource/Resource Paths
  • Add the Resource Path asrc
  • Press Apply

image31 image31

  • Open Properties/actifsource/Built-in Dependencies
  • Add Builtin CIP
  • Add Builtin DATATYPE
  • Important: Press Apply

image32 image32

  • Open Properties/actifsource/Target Folders
  • Add the existing folder src as target folder
  • Add the BuildConfig CIP_C to the target src
  • Press OK

image33 image33

  • Select the Resource Folder asrc in the Project Explorer
  • Select CipSystem from the New Resource Tool in the Eclipse toolbar

image34 image34

  • Enter ch.actifsource.tutorial.cip.arduino as Namespace
  • Enter LampSystem as Name
  • Press Finish

image35 image35

image36 image36

  • Create a new Implementation named LampSystemImplementation in your LampSystem
  • Create a new ImplementationUnit named LampSystemUnit in your LampSystemImplementation
  • Create a new CipMachine in your LampSystemUnit
  • Reference the Cluster LampCluster in your CipMachine
  • Create a new CipShell in your LampSystemUnit
  • Reference the input channel Button
  • Reference the output channel Lamp
  • Create a new C_CodeOption named LampSystem_CodeOption using Content Assist (Ctrl+Space)

image37 image37

  • Configure the newly created LampSystem_CodeOption
  • Set useInterface to useMessageInterface
  • Set usePostFix to InitPostFix
  • Set enable_PENDING_Information to true
  • The code for your CIP system is now generated in the target folder src
#include <avr/io.h>
#include <util/delay.h>
#include "sLampUnit.h"

#define LED_PIN     PB5   // LED connected to PB5 (Arduino Pin 13)
#define BUTTON_PIN  PB4   // Button connected to PB4 (Arduino Pin 12)

void AI_LampUnit_C2_Dark() {
  PORTB &= ~_BV(LED_PIN); // Turn LED OFF
}
void AI_LampUnit_C2_Bright() {
  PORTB |= _BV(LED_PIN);  // Turn LED ON
}
void iMSG_LampUnit() {
  OUT_LampUnit.C2_Dark = AI_LampUnit_C2_Dark;
  OUT_LampUnit.C2_Bright = AI_LampUnit_C2_Bright;
}

int main(void)
{
  if (!fINIT_LampUnit()) {return 1;} // init cip
  // Set LED_PIN as an output
  DDRB |= _BV(LED_PIN);

  // Set BUTTON_PIN as an input and enable internal pull-up resistor
  DDRB &= ~_BV(BUTTON_PIN);  // Configure as input
  PORTB |= _BV(BUTTON_PIN);  // Activate internal pull-up (idle state = HIGH)

  int oldbuttonState = 0;

  while (1) {
  int newButtonState = PINB & _BV(BUTTON_PIN); // read button pin
  TRG_LampUnit.TICK_();
  // ticking time
  if(oldbuttonState!=newButtonState && newButtonState==1) {
    IN_LampUnit.C1_Push();
  }
  if(oldbuttonState!=newButtonState && newButtonState==0) {
    IN_LampUnit.C1_Release();
  }

  oldbuttonState=newButtonState;

  while (TRG_LampUnit.PENDING_.ANY_) {
    TRG_LampUnit.STEP_();
  }
    delay_ms(4);
  }

  return 0; // Never reached
}
  • Rewrite your main.c as shown above
Info

Your code is probably not linking if you named your file main.cpp instead if main.c

image38 image38

  • Download your code to the Arduino

    • Select your project SimpleProject in the Project Explorer
    • Press the AVR download button in the Eclipse toolbar image26 image26

image39 image39

  • We built a lamp with a delayed off switch

    • Pin 2 on GND means Button is released
    • Pin 2 on 5V means Button is pressed
  • Start while Button is released

  • Press Button 🡪 Light should switch on

  • Release Button 🡪 Light should switch off with delay

    • Tune with the delay function of your timer and the delay_ms() call in the main.c

actifsource-point-large