Skip to content
This repository has been archived by the owner on Mar 20, 2022. It is now read-only.

Please move into a single manageable item #6

Open
wants to merge 242 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
242 commits
Select commit Hold shift + click to select a range
7b35ad4
hmi
Jul 11, 2016
5e0a886
Test commit
Jul 11, 2016
d876fcc
Renamed worlds and added attacks and start script to oil dir
Jul 11, 2016
ec96b41
world.py now runs listening on network instead of localhost
Jul 11, 2016
9bd9e9d
new hmi
Jul 11, 2016
80735a3
new oil hmi
Jul 12, 2016
fc0b04c
revision hmi & display
Jul 20, 2016
0939c1a
7/25-edit
Jul 25, 2016
220ee1c
Testing collab
siesta888 Jul 25, 2016
ef19d19
remove ike-test.txt
siesta888 Jul 25, 2016
c0748c0
Fixed startup script
siesta888 Jul 25, 2016
eb3a1aa
Removing inlet valve
siesta888 Jul 25, 2016
3a1330a
Test commit from c9
siesta888 Jul 25, 2016
1385650
remove test
siesta888 Jul 25, 2016
27ac72c
Removed inlet valve
siesta888 Jul 25, 2016
e723b05
Fixed nozzle
siesta888 Jul 25, 2016
a5e2819
fix
siesta888 Jul 25, 2016
1bb8fff
Renamed nozzle to pump, remove discharge pump
siesta888 Jul 25, 2016
0206a78
Fix line 297 TAG_PUMP
siesta888 Jul 25, 2016
7093c00
Fix tag
siesta888 Jul 25, 2016
c942bd4
minor text fixes
siesta888 Jul 25, 2016
c8fff7b
Fixed id
siesta888 Jul 25, 2016
4075010
run at startup
siesta888 Jul 25, 2016
ffb7d22
fixes
siesta888 Jul 25, 2016
36664c4
change to 0x10
siesta888 Jul 25, 2016
c4d0ca8
Rewriting setProcess
siesta888 Jul 25, 2016
0274d45
working on setProcess
siesta888 Jul 25, 2016
840ab54
Changing set process
siesta888 Jul 25, 2016
976bee2
Fix
siesta888 Jul 25, 2016
91c932d
Fix
siesta888 Jul 25, 2016
2a4bbbd
Added new setCommand func
siesta888 Jul 25, 2016
2b503f8
Added new setCommand func
siesta888 Jul 25, 2016
957c027
Added new setCommand func
siesta888 Jul 25, 2016
71c494e
Added new setCommand func
siesta888 Jul 25, 2016
f4eaa52
Added new setCommand func
siesta888 Jul 25, 2016
754aa24
Each process now has own func
siesta888 Jul 25, 2016
33b4754
Each process now has own func
siesta888 Jul 25, 2016
0dcc065
Add and del valve
siesta888 Jul 25, 2016
54b80b2
Add and del valve
siesta888 Jul 25, 2016
c6f00dc
Add and del valve
siesta888 Jul 25, 2016
c00d165
Add and del valve
siesta888 Jul 25, 2016
e025bdc
Add and del valve
siesta888 Jul 25, 2016
3e5a715
Add and del valve
siesta888 Jul 25, 2016
304e9ad
Add and del valve
siesta888 Jul 25, 2016
24851f8
Add and del valve
siesta888 Jul 25, 2016
e82c1e3
Add and del valve
siesta888 Jul 25, 2016
94c6616
Add and del valve
siesta888 Jul 25, 2016
591d640
Add and del valve
siesta888 Jul 25, 2016
c08f9da
Add and del valve
siesta888 Jul 25, 2016
d017746
Add and del valve
siesta888 Jul 25, 2016
85f8756
Add and del valve
siesta888 Jul 25, 2016
846bc20
Add and del valve
siesta888 Jul 25, 2016
ffac06e
fixing markup
siesta888 Jul 25, 2016
803aa86
fixing markup
siesta888 Jul 25, 2016
6179731
fixing markup
siesta888 Jul 25, 2016
1ea0587
fixing markup
siesta888 Jul 25, 2016
6810add
fixing markup
siesta888 Jul 25, 2016
a5ffa31
fixing markup
siesta888 Jul 25, 2016
5897166
fixing markup
siesta888 Jul 25, 2016
af82d02
fixing markup
siesta888 Jul 25, 2016
af737c9
fixing markup
siesta888 Jul 25, 2016
e605dcd
fixing markup
siesta888 Jul 25, 2016
468b110
fixing markup
siesta888 Jul 25, 2016
f7226c1
Fixed pump logic
siesta888 Jul 25, 2016
5637314
Fixed pump logic
siesta888 Jul 25, 2016
b9519a8
Fixed pump logic
siesta888 Jul 25, 2016
68af7b6
Fixed pump logic
siesta888 Jul 25, 2016
cccb08e
fixed oil storage lines
siesta888 Jul 26, 2016
d6996ec
start/stop
siesta888 Jul 26, 2016
cbecf68
changed registers
siesta888 Jul 26, 2016
08762c0
fix pump status message
siesta888 Jul 26, 2016
9444989
fix pump status message
siesta888 Jul 26, 2016
c537026
fix pump status message
siesta888 Jul 26, 2016
089ceff
fix pump status message
siesta888 Jul 26, 2016
f77e40e
fix pump status message
siesta888 Jul 26, 2016
da1d040
fix pump status message
siesta888 Jul 26, 2016
a677d24
fix pump status message
siesta888 Jul 26, 2016
fc096db
remove valve
siesta888 Jul 26, 2016
6dd57eb
remove valve
siesta888 Jul 26, 2016
8ddaa71
remove valve
siesta888 Jul 26, 2016
4d4a34a
remove valve
siesta888 Jul 26, 2016
f8cf578
add sep handler
siesta888 Jul 26, 2016
f568d9a
add sep handler
siesta888 Jul 26, 2016
2cbe3b6
add sep handler
siesta888 Jul 26, 2016
7b0c9a8
add sep handler
siesta888 Jul 26, 2016
a125c34
add sep handler
siesta888 Jul 26, 2016
7c4f56f
removed outlet valve
siesta888 Jul 26, 2016
e3f55eb
added attack scripts
Jul 27, 2016
9d55cb2
adding test scripts
Jul 28, 2016
f0f9649
sensor add
siesta888 Jul 28, 2016
378d3a3
Merge branch 'master' of github.com:bealerjm/virtuaplant
siesta888 Jul 28, 2016
8942d47
separator sensors
Jul 28, 2016
5832785
stash
siesta888 Jul 28, 2016
edf84fb
fix
siesta888 Jul 28, 2016
ae0a3b2
Cleaned up code, removed legacy code, added comments, simplified
siesta888 Jul 28, 2016
0044e22
minor changes
siesta888 Jul 28, 2016
24638f3
Pump should turn on automatically now
siesta888 Jul 28, 2016
e54b742
Fixing auto pump
siesta888 Jul 28, 2016
31c92be
working on logic
siesta888 Jul 28, 2016
744bd5b
working on logic
siesta888 Jul 28, 2016
b7690e0
working on logic
siesta888 Jul 28, 2016
0d9dd62
working on logic
siesta888 Jul 28, 2016
921294d
working on logic
siesta888 Jul 28, 2016
deff38c
working on logic
siesta888 Jul 28, 2016
e4ba0a0
adding manual control for level sensor
siesta888 Jul 28, 2016
dffde49
Added manual level control
siesta888 Jul 28, 2016
0fcd76d
Added manual level control
siesta888 Jul 28, 2016
a5882ce
Added manual level control
siesta888 Jul 28, 2016
01224f4
Fixed attack scripts, other minor changes, added todo list
siesta888 Jul 28, 2016
e47b303
Testing command line args on nothing_runs.py
siesta888 Jul 28, 2016
0b3705e
Fixed: args.target
siesta888 Jul 28, 2016
cd58309
Fancifying attack scripts
siesta888 Jul 28, 2016
4de34c6
Fancifying attack scripts
siesta888 Jul 28, 2016
7b25ab5
Fancifying attack scripts
siesta888 Jul 28, 2016
6d85d02
Added arg parse to attack scripts and hmi and world. Changed start.sh…
siesta888 Jul 29, 2016
19e4455
minor fix to server_addr var
siesta888 Jul 29, 2016
72dd616
Fixing args var
siesta888 Jul 29, 2016
f009d36
Fixing args var
siesta888 Jul 29, 2016
47baa5c
Finalizing
siesta888 Jul 29, 2016
1c9dd64
fixed separator pipe
siesta888 Jul 29, 2016
86ddedb
title size changed
siesta888 Jul 29, 2016
ece0138
screen placement
siesta888 Jul 29, 2016
6b31bc4
changed sensor colors
siesta888 Jul 30, 2016
150c5c0
Adding oil processed/spilled
siesta888 Aug 2, 2016
814ab62
Adding oil processed/spilled
siesta888 Aug 2, 2016
4f0f159
Adding oil processed/spilled
siesta888 Aug 2, 2016
ae586ef
Adding oil processed/spilled
siesta888 Aug 2, 2016
225080e
Adding oil processed/spilled
siesta888 Aug 2, 2016
16a420d
Adding sensor for spilled oil
siesta888 Aug 2, 2016
0ade6f0
Adding sensor for spilled oil
siesta888 Aug 2, 2016
49e1b04
Adding sensor for spilled oil
siesta888 Aug 2, 2016
2f30355
Adding sensor for spilled oil
siesta888 Aug 2, 2016
08d7ffb
Adding sensor for spilled oil
siesta888 Aug 2, 2016
b4b2855
Adding sensor for spilled oil
siesta888 Aug 2, 2016
7d03b10
Adding sensor for spilled oil
siesta888 Aug 2, 2016
61a37dc
Adding sensor for spilled oil
siesta888 Aug 2, 2016
20c6156
Adding sensor for spilled oil
siesta888 Aug 2, 2016
a6294aa
Adding sensor for spilled oil
siesta888 Aug 2, 2016
4349507
Adding sensor for spilled oil
siesta888 Aug 2, 2016
2d808df
Adding sensor for spilled oil
siesta888 Aug 2, 2016
93bf3a6
Adding sensor for spilled oil
siesta888 Aug 2, 2016
b6c80d6
Adding sensor for spilled oil
siesta888 Aug 2, 2016
68e7dfe
Adding sensor for spilled oil
siesta888 Aug 2, 2016
fb68c25
Adding sensor for spilled oil
siesta888 Aug 2, 2016
d96e4d9
Added overloaded draw_line function to accept optional color argument
siesta888 Aug 2, 2016
a7887ed
Adjusting sensor position
siesta888 Aug 2, 2016
ddaba41
Adjusting sensor position
siesta888 Aug 2, 2016
107e7f4
Display amount of oil spilled
siesta888 Aug 2, 2016
0551e8b
Display amount of oil spilled
siesta888 Aug 2, 2016
2a765d4
Display amount of oil spilled
siesta888 Aug 2, 2016
768f51e
Fixed type error concatenating strings and ints
siesta888 Aug 2, 2016
48502f2
Collision not registering
siesta888 Aug 2, 2016
fda0e56
Collision not registering
siesta888 Aug 2, 2016
6118ba5
PLC Updates are slow
siesta888 Aug 2, 2016
8f5f2a1
Changing how oil spilled/processed is registered. Use register data s…
siesta888 Aug 2, 2016
09595f2
Fixing reference before assignment
siesta888 Aug 2, 2016
63ea575
Fixing reference before assignment
siesta888 Aug 2, 2016
d1de6d1
Fixing reference before assignment
siesta888 Aug 2, 2016
8d98ac5
Fixing reference before assignment
siesta888 Aug 2, 2016
5534645
Fixing reference before assignment
siesta888 Aug 2, 2016
e636850
Changed volume labels to be liters not ml
siesta888 Aug 2, 2016
4855719
minor text fixes
siesta888 Aug 2, 2016
6f82d21
updates
Aug 10, 2016
f77c189
added outlet closed function
siesta888 Aug 10, 2016
225e1a0
added separator line
siesta888 Aug 10, 2016
7be6415
added outlet logic to hmi
siesta888 Aug 10, 2016
a19725c
world update
siesta888 Aug 10, 2016
80140d9
added pump
siesta888 Aug 10, 2016
ae54404
Converting sensors into lines
siesta888 Aug 10, 2016
ffba0ed
Converting sensors into lines
siesta888 Aug 10, 2016
ac87679
outlet label
siesta888 Aug 10, 2016
7816ecc
outlet label
siesta888 Aug 10, 2016
4ce0049
labels
siesta888 Aug 10, 2016
319bc33
labels
siesta888 Aug 10, 2016
63f9771
Converting sensors into lines
siesta888 Aug 10, 2016
1fdbd20
Converting sensors into lines
siesta888 Aug 10, 2016
8df8606
Converting sensors into lines
siesta888 Aug 10, 2016
5cd2e92
Converting sensors into lines
siesta888 Aug 10, 2016
48675d1
Converting sensors into lines
siesta888 Aug 10, 2016
14c012b
Converting sensors into lines
siesta888 Aug 10, 2016
d0571d7
Converting sensors into lines
siesta888 Aug 10, 2016
921b594
Minor typo
siesta888 Aug 10, 2016
791f882
Minor typo
siesta888 Aug 10, 2016
eccada5
Fixing screwed up logic
siesta888 Aug 10, 2016
b84c4a2
Fixing screwed up logic
siesta888 Aug 10, 2016
8a4e59c
Fixing screwed up logic
siesta888 Aug 10, 2016
ac84e6f
Fixing screwed up logic
siesta888 Aug 10, 2016
285955b
Fixing screwed up logic
siesta888 Aug 10, 2016
3e92873
Fixing screwed up logic
siesta888 Aug 10, 2016
07c76cb
Fixing screwed up logic
siesta888 Aug 10, 2016
7662eaf
changed ball oil size
siesta888 Aug 10, 2016
c999bd2
Fixing screwed up logic
siesta888 Aug 10, 2016
e9e95d2
Added/Fixed separator valve
siesta888 Aug 10, 2016
148a6be
Added/Fixed separator valve
siesta888 Aug 10, 2016
6c38d0c
Added/Fixed separator valve
siesta888 Aug 10, 2016
25217d9
Added/Fixed separator valve
siesta888 Aug 10, 2016
176c4b1
Added/Fixed separator valve
siesta888 Aug 10, 2016
4d5e6de
Added/Fixed separator valve
siesta888 Aug 10, 2016
75fd928
Added/Fixed separator valve
siesta888 Aug 10, 2016
4a3a38a
Added/Fixed separator valve
siesta888 Aug 10, 2016
6915f2e
Added/Fixed separator valve
siesta888 Aug 10, 2016
ad66bca
Added/Fixed separator valve
siesta888 Aug 10, 2016
5548d92
Added/Fixed separator valve
siesta888 Aug 10, 2016
f754576
Added/Fixed separator valve
siesta888 Aug 10, 2016
ac4d4a6
Waste Valve
siesta888 Aug 10, 2016
44335b4
Waste Valve
siesta888 Aug 10, 2016
4dc2da5
Waste Valve
siesta888 Aug 10, 2016
05f5d68
Fixing Valve
siesta888 Aug 10, 2016
fa6ac1b
Fixing Valve
siesta888 Aug 11, 2016
5ed4b3c
Fixing Valve
siesta888 Aug 11, 2016
634d9cf
Fixing Valve
siesta888 Aug 11, 2016
a374701
Fixing Valve
siesta888 Aug 11, 2016
e3d8a70
Fixing Valve
siesta888 Aug 11, 2016
86dfd32
Fixing Valve
siesta888 Aug 11, 2016
55be63a
Fixing Valve
siesta888 Aug 11, 2016
a6ae1ae
Fixing Valve
siesta888 Aug 11, 2016
3d94ffc
separator vessel
siesta888 Aug 11, 2016
15c268b
Added attack script that fakes no oil spilling
siesta888 Aug 11, 2016
cc9c300
separator vessel
siesta888 Aug 11, 2016
1fbac45
separator vessel
siesta888 Aug 11, 2016
55902fb
separator vessel
siesta888 Aug 11, 2016
2808f9f
separator vessel
siesta888 Aug 11, 2016
ac8c98e
separator vessel
siesta888 Aug 11, 2016
65264ad
Added attack script that fakes no oil spilling
siesta888 Aug 11, 2016
13e5220
Friction
siesta888 Aug 11, 2016
35f370c
attack script changes
Aug 11, 2016
8cf3882
oil unit height
Aug 11, 2016
4d0812a
adding oil process sensor, fixing oil spill
siesta888 Sep 8, 2016
c4caa1a
Merge branch 'master' of github.com:bealerjm/virtuaplant
siesta888 Sep 8, 2016
c6646e9
changing sensor colors
siesta888 Sep 8, 2016
31293f1
changing sensor colors
siesta888 Sep 8, 2016
77a7df0
changing sensor colors
siesta888 Sep 8, 2016
dcdfa83
changing sensor colors
siesta888 Sep 8, 2016
f6de7a9
fixing process sensor
siesta888 Sep 8, 2016
eba0523
fixing process sensor
siesta888 Sep 8, 2016
593d5cd
fixing process sensor
siesta888 Sep 8, 2016
9d4e3c7
fixing process sensor
siesta888 Sep 8, 2016
70dd7da
fixing process sensor
siesta888 Sep 8, 2016
f6e3849
fixing process sensor
siesta888 Sep 8, 2016
3796138
fixing process sensor
siesta888 Sep 8, 2016
4acb027
fixing process sensor
siesta888 Sep 8, 2016
323569e
Developing fix for 16 bit register limit on oil processed amount. Upp…
siesta888 Jan 31, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plants/bottle-filling/attacks/stop_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
try:
client.connect()
while True:
rq = client.write_register(0x10, 1) # Run Plant, Run!
rq = client.write_register(0x01, 1) # Run Plant, Run!
rq = client.write_register(0x1, 0) # Level Sensor
rq = client.write_register(0x2, 1) # Limit Switch
rq = client.write_register(0x3, 0) # Motor
Expand Down
2 changes: 1 addition & 1 deletion plants/bottle-filling/hmi.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,4 @@ def app_main():
if __name__ == "__main__":
GObject.threads_init()
app_main()
Gtk.main()
Gtk.main()
2 changes: 1 addition & 1 deletion plants/bottle-filling/world.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,4 +351,4 @@ def main():
startModbusServer()

if __name__ == '__main__':
sys.exit(main())
sys.exit(main())
354 changes: 354 additions & 0 deletions plants/bottle-filling/world.py~
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
#!/usr/bin/env python

#########################################
# Imports
#########################################
# - Logging
import logging

# - Multithreading
from twisted.internet import reactor

# - Modbus
from pymodbus.server.async import StartTcpServer
from pymodbus.device import ModbusDeviceIdentification
from pymodbus.datastore import ModbusSequentialDataBlock
from pymodbus.datastore import ModbusSlaveContext, ModbusServerContext
from pymodbus.transaction import ModbusRtuFramer, ModbusAsciiFramer

# - World Simulator
import sys, random
import pygame
from pygame.locals import *
from pygame.color import *
import pymunk

#########################################
# Logging
#########################################
logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.INFO)

#########################################
# Util Functions
#########################################
def PLCSetTag(addr, value):
context[0x0].setValues(3, addr, [value])

def PLCGetTag(addr):
return context[0x0].getValues(3, addr, count=1)[0]

#########################################
# World Code
#########################################

# "Constants"
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 350
FPS=50.0

MODBUS_SERVER_PORT=5020

PLC_TAG_LEVEL_SENSOR = 0x1
PLC_TAG_LIMIT_SWITCH = 0x2
PLC_TAG_MOTOR = 0x3
PLC_TAG_NOZZLE = 0x4
PLC_TAG_RUN = 0x10

# Global Variables
global bottles
bottles = []

def to_pygame(p):
"""Small hack to convert pymunk to pygame coordinates"""
return int(p.x), int(-p.y+600)

# Shape functions

def add_ball(space):
mass = 0.01
radius = 3
inertia = pymunk.moment_for_circle(mass, 0, radius, (0,0))
body = pymunk.Body(mass, inertia)
body._bodycontents.v_limit = 120
body._bodycontents.h_limit = 1
x = random.randint(181,182)
body.position = x, 410
shape = pymunk.Circle(body, radius, (0,0))
shape.collision_type = 0x5 #liquid
space.add(body, shape)
return shape

def draw_ball(screen, ball, color=THECOLORS['blue']):
p = int(ball.body.position.x), 600-int(ball.body.position.y)
pygame.draw.circle(screen, color, p, int(ball.radius), 2)

def add_bottle_in_sensor(space):

body = pymunk.Body()
body.position = (40, 300)
radius = 2
shape = pymunk.Circle(body, radius, (0, 0))
shape.collision_type = 0x7 # 'bottle_in'
space.add(shape)
return shape

def add_level_sensor(space):

body = pymunk.Body()
body.position = (155, 380)
radius = 3
shape = pymunk.Circle(body, radius, (0, 0))
shape.collision_type = 0x4 # level_sensor
space.add(shape)
return shape

def add_limit_switch(space):

body = pymunk.Body()
body.position = (200, 300)
radius = 2
shape = pymunk.Circle(body, radius, (0, 0))
shape.collision_type = 0x1 # switch
space.add(shape)
return shape

def add_nozzle(space):

body = pymunk.Body()
body.position = (180, 430)
shape = pymunk.Poly.create_box(body, (15, 20), (0, 0), 0)
space.add(shape)
return shape

def add_base(space):

body = pymunk.Body()
body.position = (0, 300)
shape = pymunk.Poly.create_box(body, (SCREEN_WIDTH, 20), ((SCREEN_WIDTH/2), -10), 0)
shape.friction = 1.0
shape.collision_type = 0x6 # base
space.add(shape)
return shape

def add_bottle(space):
mass = 10
inertia = 0xFFFFFFFFF
body = pymunk.Body(mass, inertia)
body.position = (130,300)
l1 = pymunk.Segment(body, (-150, 0), (-100, 0), 2.0)
l2 = pymunk.Segment(body, (-150, 0), (-150, 100), 2.0)
l3 = pymunk.Segment(body, (-100, 0), (-100, 100), 2.0)

# Glass friction
l1.friction = 0.94
l2.friction = 0.94
l3.friction = 0.94

# Set collision types for sensors
l1.collision_type = 0x2 # bottle_bottom
l2.collision_type = 0x3 # bottle_side
l3.collision_type = 0x3 # bottle_side

space.add(l1, l2, l3, body)
return l1,l2,l3

def draw_polygon(screen, shape):

points = shape.get_vertices()
fpoints = []
for p in points:
fpoints.append(to_pygame(p))
pygame.draw.polygon(screen, THECOLORS['black'], fpoints)


def draw_lines(screen, lines, color=THECOLORS['dodgerblue4']):
"""Draw the lines"""
for line in lines:
body = line.body
pv1 = body.position + line.a.rotated(body.angle)
pv2 = body.position + line.b.rotated(body.angle)
p1 = to_pygame(pv1)
p2 = to_pygame(pv2)
pygame.draw.lines(screen, color, False, [p1,p2])

# Collision handlers
def no_collision(space, arbiter, *args, **kwargs):
return False

def level_ok(space, arbiter, *args, **kwargs):

log.debug("Level reached")
PLCSetTag(PLC_TAG_LIMIT_SWITCH, 0) # Limit Switch Release, Fill Bottle
PLCSetTag(PLC_TAG_LEVEL_SENSOR, 1) # Level Sensor Hit, Bottle Filled
PLCSetTag(PLC_TAG_NOZZLE, 0) # Close nozzle
return False

def bottle_in_place(space, arbiter, *args, **kwargs):

log.debug("Bottle in place")
PLCSetTag(PLC_TAG_LIMIT_SWITCH, 1)
PLCSetTag(PLC_TAG_LEVEL_SENSOR, 0)
PLCSetTag(PLC_TAG_NOZZLE, 1) # Open nozzle
return False

def add_new_bottle(space, arbiter, *args, **kwargs):
global bottles
bottles.append(add_bottle(space))
log.debug("Adding new bottle")
return False

def runWorld():
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Bottle-Filling Factory - World View - VirtuaPlant")
clock = pygame.time.Clock()
running = True

space = pymunk.Space()
space.gravity = (0.0, -900.0)

# Limit switch with bottle bottom
space.add_collision_handler(0x1, 0x2, begin=bottle_in_place)
# Level sensor with water
space.add_collision_handler(0x4, 0x5, begin=level_ok)
# Level sensor with ground
space.add_collision_handler(0x4, 0x6, begin=no_collision)
# Limit switch with ground
space.add_collision_handler(0x1, 0x6, begin=no_collision)
# Limit switch with bottle side
space.add_collision_handler(0x1, 0x3, begin=no_collision)
# Level sensor with bottle side
space.add_collision_handler(0x4, 0x3, begin=no_collision)
# Bottle in with bottle sides and bottom
space.add_collision_handler(0x7, 0x2, begin=no_collision, separate=add_new_bottle)
space.add_collision_handler(0x7, 0x3, begin=no_collision)

base = add_base(space)
nozzle = add_nozzle(space)
limit_switch = add_limit_switch(space)
level_sensor = add_level_sensor(space)
bottle_in = add_bottle_in_sensor(space)

global bottles
bottles.append(add_bottle(space))

balls = []

ticks_to_next_ball = 1

fontBig = pygame.font.SysFont(None, 40)
fontMedium = pygame.font.SysFont(None, 26)
fontSmall = pygame.font.SysFont(None, 18)

while running:
clock.tick(FPS)

for event in pygame.event.get():
if event.type == QUIT:
running = False
elif event.type == KEYDOWN and event.key == K_ESCAPE:
running = False

screen.fill(THECOLORS["white"])

if PLCGetTag(PLC_TAG_RUN):

# Motor Logic
if (PLCGetTag(PLC_TAG_LIMIT_SWITCH) == 1):
PLCSetTag(PLC_TAG_MOTOR, 0)

if (PLCGetTag(PLC_TAG_LEVEL_SENSOR) == 1):
PLCSetTag(PLC_TAG_MOTOR, 1)

ticks_to_next_ball -= 1

if not PLCGetTag(PLC_TAG_LIMIT_SWITCH):
PLCSetTag(PLC_TAG_MOTOR, 1)

if ticks_to_next_ball <= 0 and PLCGetTag(PLC_TAG_NOZZLE):
ticks_to_next_ball = 1
ball_shape = add_ball(space)
balls.append(ball_shape)

# Move the bottles
if PLCGetTag(PLC_TAG_MOTOR) == 1:
for bottle in bottles:
bottle[0].body.position.x += 0.25
else:
PLCSetTag(PLC_TAG_MOTOR, 0)

# Draw water balls
# Remove off-screen balls
balls_to_remove = []
for ball in balls:
if ball.body.position.y < 150 or ball.body.position.x > SCREEN_WIDTH+150:
balls_to_remove.append(ball)

draw_ball(screen, ball)

for ball in balls_to_remove:
space.remove(ball, ball.body)
balls.remove(ball)

# Draw bottles
for bottle in bottles:
if bottle[0].body.position.x > SCREEN_WIDTH+150 or bottle[0].body.position.y < 150:
space.remove(bottle, bottle[0].body)
bottles.remove(bottle)
continue
draw_lines(screen, bottle)

# Draw the base and nozzle
draw_polygon(screen, base)
draw_polygon(screen, nozzle)
# Draw the limit switch
draw_ball(screen, limit_switch, THECOLORS['green'])
# Draw the level sensor
draw_ball(screen, level_sensor, THECOLORS['red'])

title = fontMedium.render(str("Bottle-filling factory"), 1, THECOLORS['deepskyblue'])
name = fontBig.render(str("VirtuaPlant"), 1, THECOLORS['gray20'])
instructions = fontSmall.render(str("(press ESC to quit)"), 1, THECOLORS['gray'])
screen.blit(title, (10, 40))
screen.blit(name, (10, 10))
screen.blit(instructions, (SCREEN_WIDTH-115, 10))

space.step(1/FPS)
pygame.display.flip()

# Stop reactor if running
if reactor.running:
reactor.callFromThread(reactor.stop)

#########################################
# Modbus Server Code
#########################################

store = ModbusSlaveContext(
di = ModbusSequentialDataBlock(0, [0]*100),
co = ModbusSequentialDataBlock(0, [0]*100),
hr = ModbusSequentialDataBlock(0, [0]*100),
ir = ModbusSequentialDataBlock(0, [0]*100))

context = ModbusServerContext(slaves=store, single=True)

identity = ModbusDeviceIdentification()
identity.VendorName = 'MockPLCs'
identity.ProductCode = 'MP'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'MockPLC 3000'
identity.ModelName = 'MockPLC Ultimate'
identity.MajorMinorRevision = '1.0'

def startModbusServer():

StartTcpServer(context, identity=identity, address=("localhost", MODBUS_SERVER_PORT))

def main():
reactor.callInThread(runWorld)
startModbusServer()

if __name__ == '__main__':
sys.exit(main())
Loading