Aller au contenu

Tests un peu plus poussés de la cam du raspberry.


Greg34

Messages recommandés

Bonsoir,

 

Histoire de causer et de reprendre le fil de la conversation (http://www.webastro.net/forum/showthread.php?t=124294).

Je me suis amusé à pousser le gadget ce soir un peu plus.

 

Bref, c'est pas une caméra astro (on le savait déjà ;)) mais n'ayant que ça sous la main pour le moment, je pense que d'autres amateurs comme moi auront le même vice à tester l'impossible :p

 

L'original (6s d'expo):

qzghlx.jpg

 

Et un essai sous RegiStax 6 pour la forme avec 8 autres images...

24xqjx2.jpg

 

Faut de-zoomer les images...trop près ça craint :be:

Lien vers le commentaire
Partager sur d’autres sites

  • 2 mois plus tard...

Salut Look!

 

J'ai un script qui traine sur le DD qui pourrait être un bon point de départ:

(Désolé pour l'auteur, je me souviens plus qui l'a écrit:wub:)

#!/usr/bin/python
import os
import struct
import pygame, sys
import datetime
import time
import math
from pygame.locals import *
import pygame.camera
import shutil
import random
import numpy
import subprocess, glob
import RPi.GPIO as GPIO
import signal
from decimal import *
getcontext().prec = 8



#ONLY WORKS WITH PYTHON 2.7

# START BY USING 'startx' at the prompt THEN ON THE GUI OPEN LXTerminal AND ENTER 'sudo idle'
# then open the file, choose 'run', and 'run module'

#==================================================================================================
#FOR TESTING / simulation ONLY, 1 for normal use
# setting camera_connected = 0 will run in demo / simulation mode
camera_connected = 0

#==================================================================================================
# set serial_connected = 0 if you don't have the Arduino Uno etc connected.
# ensure you install pyserial (sudo apt-get install python-serial) if not already installed.
serial_connected = 0

# if using the RPi camera set use_Pi_Cam = 1 , if set to 0 the program will use a USB camera.
use_Pi_Cam = 0

# if using the RPi.GPIO on the Pi set use_RPiGPIO = 1.
use_RPiGPIO = 0

#==================================================================================================
# DISPLAY SETTINGS

# Display - Set this dependent on your display. 0 = LCD 840x480, 1 = PAL Composite (640x480),
# 2 = other, set by parameters below
# if using 1 for PAL composite then adjust /boot/config.txt to suit
# if using 0 then change /boot/config.txt for hmdi group = 2 and hdmi mode = 14 
Display = 0

# Image_window - sets image window size, either 0 = 320x240, 1 = 352x288,2 = 640x480, 3 = 800x600
# Any for HDMI but recommend 0 for Composite (PAL)
Image_window = 0

# max_res - camera max available resolution, depends on your webcam
# 0 = 320x240, 1 = 352x288, 2 = 640x480, 3 = 800x600, 4 = 960x720, 5 = 1280x960, 6 = 1920x1440, 7 = 2592x1944
# Logitech C270 won't work past max_res = 4, Philip 900 won't work above above max_res = 2, pi camera = 7
max_res = 7

# bits - bits to display in pygame
bits = 16

#==================================================================================================
# SET DEFAULT CONFIG
#==================================================================================================
# auto_g - autoguide on = 1, off = 0
auto_g = 0
# nsscale - North/South scaling in milliSecs/pixel, 150 for 400mm focal length guide scope
nscale = 150
sscale = 150
# ewscale - East / West scaling in milliSecs/pixel, 150 for 400mm focal length guide scope
escale = 150
wscale = 150
# ewi - Invert East <> West invert = 1, non-invert = 0
ewi = 0
# nsi - Invert North<>South invert = 1, non-invert = 0
nsi = 0
# crop - Tracking Window size in pixels. 
crop = 30
# minwin - set minimum Tracking window size 
minwin = 20
# maxwin - set maximum Tracking window size
maxwin = 100
# offset3/4 - Tracking Window offset from centre of screen
offset3 = 0
offset4 = 0
# Intervals - Guiding Interval in seconds (approx)
Intervals = 2
# log - Log commands to .txt file, on = 1, off = 0
log = 0
# frames - RGBW = 1234
frames = 1
# Sens - sensitivity (Threshold)
Sens = 20
# threshold - on = 1,off = 0. Displays detected star pixels
thres = 0
# graph - on = 1, off = 0. shows brightness of star, and threshold value
graph = 0
# plot - plot movements, on = 1, off = 0
plot = 0
# auto_win - auto size tracking window, on = 1, off = 0
auto_win = 0
# auto_wlos - auto window increase on loss of guide star, when using auto_win
auto_wlos = 1
# auto_t - auto threshold, on = 1, off = 0
auto_t = 0
#crop_img - set to give cropped image, higher magnification
crop_img = 0
#auto_i - auto-interval, on = 1, off = 0
auto_i = 0

# PHOTO Settings for external camera photos
#===================================================================================
# photoon - enable function = 1, needs use_RPiGPIO = 1 for output to work
photoon = 1
# pwait - wait in seconds between multiple photos
pwait = 5
#ptime - exposure time in seconds
ptime = 60
#pcount - number of photos to take
pcount = 1

# RPi camera presets
#===================================================================================
rpico = 90
rpibr = 70
rpiexno = 0
rpiISO = 800
rpiev = 0
rpisa =  0
rpiss = 100000
rpit = 0

rpimodes = ['off', 'auto', 'night', 'backlight', 'spotlight', 'sports', 'snow', 'verylong', 'fireworks']
rpimodesa = [' off', 'auto', 'night', 'backl', 'spotl', 'sport', 'snow', 'vlong', 'fwork']
rpiwidth = [320,352,640,800,960,1280,1920,2592]
rpiheight = [240,288,480,600,720,960,1440,1944]
rpiscalex = [1,1.1,1.818,1.25,1.2,1.333,1.5,1.35]
rpiscaley = [1,1.2,1.666,1.25,1.2,1.333,1.5,1.35]

vtime = 0
htime = 0
ptime2 = 0
rpiex = rpimodes[rpiexno]


#===================================================================================
#SETUP GPIO
#Telescope control
#N_OP = 22
#S_OP = 18
#E_OP = 24
#W_OP = 16
# External camera control eg DSLR
#C_OP = 26

#Alternative GPIO outputs
#Telescope control
N_OP = 16
S_OP = 13
E_OP = 11
W_OP = 18
# External camera control eg DSLR
C_OP = 26

if use_RPiGPIO == 1:
  GPIO.setwarnings(False)
  GPIO.setmode (GPIO.BOARD)
  GPIO.setup(N_OP, GPIO.OUT)
  GPIO.output(N_OP,GPIO.LOW)
  GPIO.setup(S_OP, GPIO.OUT)
  GPIO.output(S_OP,GPIO.LOW)
  GPIO.setup(E_OP, GPIO.OUT)
  GPIO.output(E_OP,GPIO.LOW)
  GPIO.setup(W_OP, GPIO.OUT)
  GPIO.output(W_OP,GPIO.LOW)
  if photoon == 1:
     GPIO.setup(C_OP, GPIO.OUT)
     GPIO.output(C_OP,GPIO.LOW)

#==============================================================================
# mincor - Minimum correction (mSecs)
mincor = (nscale + sscale + wscale + escale)/4
#a_thr_scale - sets scaling factor for auto_t
global a_thr_scale
a_thr_scale = 3
#a_thr_limit - sets lower limit for auto threshold to work
a_thr_limit = 10
#==============================================================================
if serial_connected == 1:
  import serial

pygame.init()
imu=""
limg = 0
Interval = Intervals
pimg=""
scalex = 1
scaley = 1
cls = 0
ave = 0
aveon = 0
avemax = 10
photo = 0
pcount2 = 0
ptime2 = 0
camera = 0
ymax = 0
z = 0
shift = 0
change = 1
achange = 0
fc = 0
mxo = []
track = 0
auto_c = 0
rgb = ['X','R','G','B','W']
fontObj = pygame.font.Font(None,16)
redColor = pygame.Color(255,0,0)
greenColor = pygame.Color(0,255,0)
greyColor = pygame.Color(128,128,128)
dgryColor = pygame.Color(64,64,64)
lgryColor = pygame.Color(192,192,192)
blackColor = pygame.Color(0,0,0)
whiteColor = pygame.Color(255,255,255)
purpleColor = pygame.Color(255,0,255)
yellowColor = pygame.Color(255,255,0)

if Image_window > crop_img:
  crop_img = Image_window


if Display == 0:
  width = 640
  b1x = width
  b1y = -32
  b2x = width
  b2y = 129
  b3x = width
  b3y = 289
  Image_window = 2
  height = 480
  hplus = 0
  crop_img = 2

if Display == 1:
  width = 352
  height = 288
  b1x = 0
  b1y = height
  b2x = 192
  b2y = height
  b3x = 385
  b3y = height
  hplus = 192
  Image_window = 1
  modewidth = 640
  crop_img = 1
  hplus = 192

if Display > 1:
  width = rpiwidth[image_window]
  height = rpiheight[image_window]
  b1x = 0
  b1y = height
  b2x = 192
  b2y = height
  b3x = 385
  b3y = height
  hplus = 192

min_res = Image_window

while z <= crop_img:
  scalex = scalex * rpiscalex[z]
  scaley = scaley * rpiscaley[z]
  z +=1

if crop_img > 0:
  nscale = int(nscale/scaley)
  escale = int(escale/scalex)
  sscale = int(sscale/scaley)
  wscale = int(wscale/scalex)
offset5 = 0
offset6 = 0
offset7 = 0
offset8 = 0
w = rpiwidth[crop_img]
h = rpiheight[crop_img]
if width <= 352:
  modewidth = 640
else:
  modewidth = width + 200      
windowSurfaceObj = pygame.display.set_mode((modewidth,height + hplus),1,bits)
pygame.display.set_caption('AutoGuider 90')


if camera_connected == 1 and use_Pi_Cam == 0:
  pygame.camera.init()
  if crop_img == 0:
     cam = pygame.camera.Camera("/dev/video0",(320,240))
  if crop_img == 1 and max_res >= 1:
     cam = pygame.camera.Camera("/dev/video0",(352,288))
  if crop_img == 2 and max_res >= 2:
     cam = pygame.camera.Camera("/dev/video0",(640,480))
  if crop_img == 3 and max_res >= 3:
     cam = pygame.camera.Camera("/dev/video0",(800,600))
  if crop_img == 4 and max_res >= 4:
     cam = pygame.camera.Camera("/dev/video0",(960,720))
  if crop_img == 5 and max_res >= 5:
     cam = pygame.camera.Camera("/dev/video0",(1280,960))
  if crop_img == 6 and max_res >= 6:
     cam = pygame.camera.Camera("/dev/video0",(1920,1440))
  if crop_img == 7 and max_res >= 7:
     cam = pygame.camera.Camera("/dev/video0",(2592,1944))
  cam.start()
  cam.set_controls(0,0,rpibr)

if use_Pi_Cam == 1 and camera_connected == 1:
  if os.path.exists('/run/shm/test.jpg') == True:
     os.rename('/run/shm/test.jpg','/run/shm/oldtest.jpg')

  rpistr = "raspistill -o /run/shm/test.jpg -co " + str(rpico) + " -br " + str(rpibr)
  if rpiex != 'off':
     rpistr = rpistr + " -t " + str(rpit) + " -tl 0 -ex " + rpiex
  else:
     rpistr = rpistr + " -t " + str(rpit) + " -tl 0 -ss " + str(rpiss) + " -fp -bm -awb off -awbg 1,1.3"
  if rpiISO > 0:
     rpistr = rpistr + " -ISO " + str(rpiISO)
  if rpiev != 0:
     rpistr = rpistr + " -ev " + str(rpiev)
  off5 = (Decimal(0.5) - (Decimal(width)/Decimal(2))/Decimal(w)) + (Decimal(offset5)/Decimal(w))
  off6 = (Decimal(0.5) - (Decimal(height)/Decimal(2))/Decimal(h)) + (Decimal(offset6)/Decimal(h))
  widx = Decimal(width)/Decimal(w)
  heiy = Decimal(height)/Decimal(h)
  rpistr = rpistr + " -q 100 -n -sa " + str(rpisa) + " -w " + str(width) + " -h " + str(height) + " -roi " +  str(off5) + "," + str(off6) + ","+str(widx) + "," + str(heiy)
  p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)

def button2 (bx1,by1,bx2,by2,height,bColor):
  greyColor = pygame.Color(128,128,128)
  dgryColor = pygame.Color(64,64,64)
  blackColor = pygame.Color(0,0,0)
  whiteColor = pygame.Color(255,255,255)
  redColor = pygame.Color(255,0,0)
  colors = [greyColor,dgryColor,redColor]
  Color = colors[bColor]
  pygame.draw.rect(windowSurfaceObj,Color,Rect(bx1,height+by1,bx2,by2))
  pygame.draw.line(windowSurfaceObj,whiteColor,(bx1,height+by1),(bx1+bx2-1,height+by1))
  pygame.draw.line(windowSurfaceObj,whiteColor,(bx1+1,height+by1+1),(bx1+bx2-2,height+by1+1))
  #pygame.draw.line(windowSurfaceObj, greyColor,(bx1,height+by1),(bx1,height+by1+30))
  #pygame.draw.line(windowSurfaceObj, greyColor,(bx1+1,height+by1+1),(bx1+1,height+by1+29))
  return()

button2 (b1x+1,b1y+33,127,31,0,auto_g)
button2 (b1x+1,b1y+65,63,31,0,plot)
button2 (b1x+1,b1y+97,63,31,0,log)
button2 (b1x+1,b1y+129,63,31,0,graph)
button2 (b1x+1,b1y+161,63,31,0,thres)
button2 (b1x+65,b1y+65,63,31,0,auto_win)
button2 (b1x+65,b1y+97,63,31,0,auto_t)
button2 (b1x+65,b1y+129,63,31,0,auto_i)
button2 (b1x+65,b1y+161,31,31,0,0)
button2 (b1x+97,b1y+161,31,31,0,0)

cy = 33
while cy <= 161:
  button2 (b1x+129,b1y+cy,63,31,0,0)
  cy +=32



button2 (b2x+1,b2y+33,63,31,0,0)
if use_Pi_Cam == 1 and camera_connected == 1:
  cy = 65
  while cy <= 161:
     button2 (b2x+1,b2y+cy,63,31,0,0)
     cy +=32

cy = 33
while cy <= 161:
  button2 (b2x+65,b2y+cy,63,31,0,0)
  cy +=32


if photoon == 1:
  button2 (b2x+129,b2y+33,63,31,0,0)
  button2 (b2x+129,b2y+65,63,31,0,0)
  button2 (b2x+129,b2y+97,63,31,0,0)
if use_Pi_Cam == 1:
  button2 (b2x+129,b2y+129,31,31,0,0)
button2 (b2x+161,b2y+129,31,31,0,shift)

button2 (b3x+1,b3y+65,31,31,0,0)

button2 (b3x+33,b3y+33,31,31,0,0)
button2 (b3x+33,b3y+97,31,31,0,0)
button2 (b3x+65,b3y+65,31,31,0,0)
button2 (b3x+129,b3y+33,31,31,0,0)
button2 (b3x+129,b3y+97,31,31,0,0)
button2 (b3x+97,b3y+65,31,31,0,0)
button2 (b3x+161,b3y+65,31,31,0,0)
button2 (b2x+161,b2y+161,31,31,0,shift)
button2 (b3x+1,b3y+145,31,31,0,0)
button2 (b3x+33,b3y+145,31,31,0,0)
button2 (b3x+65,b3y+145,31,31,0,0)
button2 (b3x+97,b3y+145,31,31,0,0)
button2 (b3x+129,b3y+145,31,31,0,0)
button2 (b3x+161,b3y+145,31,31,0,0)




def keys2(msg,fsize,fcolor,fx,fy,upd):
  greenColor = pygame.Color(0,255,0)
  greyColor = pygame.Color(128,128,128)
  dgryColor = pygame.Color(64,64,64)
  yellowColor = pygame.Color(255,255,0)
  redColor = pygame.Color(255,0,0)
  blueColor = pygame.Color(0,0,255)
  whiteColor = pygame.Color(255,255,255)
  blackColor = pygame.Color(0,0,0)
  purpleColor = pygame.Color(255,0,255)
  colors = [dgryColor,greenColor,yellowColor,redColor,greenColor,blueColor,whiteColor,greyColor,blackColor,purpleColor]
  color = colors[fcolor]
  fontObj = pygame.font.Font('freesansbold.ttf',fsize)
  msgSurfaceObj = fontObj.render(msg, False,color)
  msgRectobj = msgSurfaceObj.get_rect()
  msgRectobj.topleft =(fx,fy)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
  windowSurfaceObj.blit(msgSurfaceObj, msgRectobj)
  if upd ==1:
     pygame.display.update(pygame.Rect(fx,fy,64,32))
  return()

keys2 (str(int(nscale)),14,3,b2x+85,b2y+47,0)
keys2 (str(int(sscale)),14,3,b2x+85,b2y+79,0)
keys2 (str(int(escale)),14,3,b2x+85,b2y+111,0)
keys2 (str(int(wscale)),14,3,b2x+85,b2y+143,0)
keys2 (str(Sens),14,3,b1x+153,b1y+111,0)
keys2 (str(Interval),14,3,b1x+153,b1y+143,0)
keys2 (str(crop_img),14,3,b1x+155,b1y+175,0)
keys2 (str(rpibr),14,3,b2x+24,b2y + 47,0)
if use_Pi_Cam == 1 and camera_connected == 1:
  keys2 (str(rpico),14,3,b2x+24,b2y + 79,0)
  keys2 (str(int(rpiss/1000)),13,3,b2x+17,b2y + 111,0)
  keys2 ((rpimodesa[rpiexno]),14,3,b2x+17,b2y + 143,0)
  keys2 (str(rpiISO),14,3,b2x+19,b2y + 175,0)
  if rpiISO == 0:
     pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+19,b2y + 175, 26, 16))
     keys2 ('off',14,3,b2x+19,b2y + 175,0)
keys2 (rgb[frames],16,frames + 2,b1x+153,b1y+47,0)
keys2 (str(crop),14,3,b1x+153,b1y+79,0)
keys2 ("A-Win",16,auto_win,b1x+75,b1y+71,0)
keys2 ("A-Thr",16,auto_t,b1x+76,b1y+103,0)
keys2 ("AutoGuide",16,auto_g,b1x+22,b1y+39,0)
keys2 ("Log",16,log,b1x+15,b1y++103,0)
keys2 ("Graph",16,graph,b1x+11,b1y+135,0)
keys2 ("Plot",16,plot,b1x+17,b1y+71,0)
keys2 ("Thres",16,thres,b1x+11,b1y+167,0)
keys2 ("NSi",14,nsi,b1x+68,b1y+169,0)
keys2 ("EWi",14,ewi,b1x+100,b1y+169,0)
keys2 ("A- Int",16,auto_i,b1x+76,b1y+136,0)
keys2 ("rgbw",14,6,b1x+143,b1y+33,0)
keys2 ("<       >",18,6,b1x+133,b1y+42,0)
keys2 ("window",14,6,b1x+134,b1y+65,0)
keys2 ("-      +",18,6,b1x+138,b1y+74,0)
keys2 ("threshold",14,6,b1x+129,b1y+97,0)
keys2 ("-      +",18,6,b1x+138,b1y+106,0)
keys2 ("interval",14,6,b1x+133,b1y+129,0)
keys2 ("-      +",18,6,b1x+138,b1y+138,0)
keys2 ("Zoom",14,6,b1x+142,b1y+162,0)
keys2 ("-      +",18,6,b1x+138,b1y+170,0)
keys2 ("scale N",14,6,b2x+70,b2y+32,0)
keys2 ("-       +",18,6,b2x+69,b2y+42,0)
keys2 ("scale S",14,6,b2x+70,b2y+64,0)
keys2 ("-       +",18,6,b2x+69,b2y+74,0)
keys2 ("scale E",14,6,b2x+70,b2y+96,0)
keys2 ("-       +",18,6,b2x+69,b2y+106,0)
keys2 ("scale W",14,6,b2x+70,b2y+128,0)
keys2 ("-       +",18,6,b2x+69,b2y+138,0)
keys2 ("scale all",14,6,b2x+66,b2y+160,0)
keys2 ("-       +",18,6,b2x+69,b2y+170,0)
keys2 ("Brightness",12,6,b2x+1,b2y+34,0)
keys2 ("-      +",18,6,b2x+9,b2y+ 42,0)

if use_Pi_Cam == 1 and camera_connected == 1:
  keys2 ("Contrast",12,6,b2x+5,b2y + 66,0)
  keys2 ("-      +",18,6,b2x+9,b2y + 74,0)
  keys2 ("Exp Time",12,6,b2x+4,b2y + 100,0)
  keys2 ("-      +",18,6,b2x+9,b2y + 106,0)
  keys2 ("ISO",12,6,b2x+20,b2y + 163,0)
  keys2 ("-      +",18,6,b2x+9,b2y + 170,0)
  keys2 ("Exp Mode",12,6,b2x+3,b2y + 129,0)
  keys2 ("<          >",16,6,b2x+3,b2y + 138,0)

hor = 0
while hor < 98:
  keys2 ("N",18,6,b3x+42+hor,b3y+38,0)
  keys2 ("E",18,6,b3x+75+hor,b3y+70,0)
  keys2 ("S",18,6,b3x+42+hor,b3y+102,0)
  keys2 ("W",18,6,b3x+8+hor,b3y+70,0)
  hor +=97
keys2 ("R1",14,6,b3x+8,b3y+150,0)
keys2 ("R2",14,6,b3x+40,b3y+150,0)
keys2 ("R3",14,6,b3x+72,b3y+150,0)
keys2 ("S1",14,6,b3x+8+96,b3y+150,0)
keys2 ("S2",14,6,b3x+40+96,b3y+150,0)
keys2 ("S3",14,6,b3x+72+96,b3y+150,0)
keys2 ("RELOAD cfg      SAVE cfg",14,6,b3x+5,b3y+175,0)
keys2 ("TELESCOPE",14,1,b3x+6,b3y+128,0)
keys2 ("WINDOW",14,1,b3x+114,b3y+128,0)
keys2 ("stop",13,7,b3x+65,b3y+105,0)
keys2 ("cen",14,7,b3x+37,b3y+65,0)
keys2 ("tre",14,7,b3x+40,b3y+77,0)
keys2 ("cen",14,7,b3x+132,b3y+65,0)
keys2 ("tre",14,7,b3x+135,b3y+77,0)
keys2 ("con",14,shift,b2x+164,b2y+164,0)
keys2 ("cap",14,shift,b2x+164,b2y+174,0)

if use_Pi_Cam == 1:
  keys2 ("pic",14,6,b2x+133,b2y+130,0)
  keys2 ("cap",14,6,b2x+133,b2y+141,0)
keys2 ("scr",14,6,b2x+165,b2y+130,0)
keys2 ("cap",14,6,b2x+165,b2y+141,0)
if photoon == 1:
   keys2 ("PHOTO",12,photo,b2x+137,b2y+35,0)
   keys2 ("P-Time",12,6,b2x+137,b2y+67,0)
   keys2 ("-       +",18,6,b2x+134,b2y + 73,0)
   keys2 (str(ptime),14,3,b2x+147,b2y + 78,0)
   keys2 ("P-Count",12,6,b2x+137,b2y+99,0)
   keys2 ("-       +",18,6,b2x+136,b2y + 105,0)
   keys2 (str(pcount),14,3,b2x+152,b2y + 110,0)

if auto_g == 1:
  button2 (b2x+129,b2y+161,31,31,0,cls)
  keys2 ("CLS",12,cls,b2x+132,b2y+170,0)

def demo(width,height,posx,posy,blankline,wd,hd):
  time.sleep(0.5)
  ad = width
  bd = height
  imu = ""
  height = 1
  line =""
  while height < posy:
     line = line + blankline
     height +=1
  imu = imu + line
  height = posy
  while height < posy + hd:
     byte = blankline[0:((posx-1)*3)]
     imu = imu + byte
     width = posx
     while width < posx + wd :
        cd = (width - posx)
        if cd <= (wd/2):
            fd = (cd )+2
        if cd > wd/2 :
            fd = ((wd - cd))+2
        dd = (height - posy)
        if dd<= (hd/2):
            l = (dd )+2
        if dd > hd/2 :
            l = ((hd - dd))+2
        fe = fd * l
        if fe > 255:
           fe = 255
        byte = chr(fe)+chr(fe)+chr(fe)
        imu = imu + byte
        width +=1
     byte = blankline[0:((ad+1)-width)*3]
     imu = imu + byte
     height +=1
  height = posy + hd
  line = ""
  while height < bd + 1:
     line = line + blankline
     height +=1
  imu = imu + line
  return (imu)

def picture (auto_win,Display,fc,shift,filno,pwait,camera,ptime2,auto_c,width,height,crop,offset3,offset4,offset5,offset6,frames,Sens,camera_connected,redline,greline,bluline,gryline,blankline,imu,mxo,auto_t,arp,crop_img,w,h,rpico,rpibr,rpit,rpiex,rpiISO,rpiss,vtime,htime,ptime,pcount2,pcount,photo,use_Pi_Cam,use_RPiGPIO):

  greyColor = pygame.Color(128,128,128)
  dgryColor = pygame.Color(64,64,64)
# take picture
  fc +=1
  if fc > 0:
     fc = 0
  if camera_connected == 1 and use_Pi_Cam == 0:
     image = cam.get_image()
     if crop_img == 0:
        offset5 = offset3
        offset6 = offset4
        if offset5 > 0 and offset5 >= (w/2)-(width/2):
           offset5 = (w/2)-(width/2)
        if offset5 < 0 and offset5 <= 0-((w/2)-(width/2)):
           offset5 = 0-((w/2)-(width/2))
        if offset6 > 0 and offset6 >= (h/2)-(height/2):
           offset6 = (h/2)-(height/2)
        if offset6 < 0 and offset6 <= 0-((h/2)-(height/2)):
           offset6 = 0-((h/2)-(height/2))
     if crop_img > 0 and crop_img != Image_window:
        strim1 = pygame.image.tostring(image,"RGB",1)
        x = ((h/2)-(height/2)) - offset6
        strt = w * 3 * x
        imb = ""
        c = 0
        stas = (((w/2) - (width/2)) + offset5) * 3
        while c < height:
           ima = strim1[strt:strt+(w*3)]
           imd = ima[stas : stas + (width*3)]
           imb = imb + imd
           strt +=(w*3)
           c +=1
        image = pygame.image.fromstring(imb,(width,height),"RGB",1)

     try:
        if shift == 1:
           filno = filno + 1
           now = datetime.datetime.now()
           timestamp = now.strftime("%y%m%d%H%M%S")
           fname = '/home/pi/pic' + str(timestamp) + "_" + str(filno) + '.jpg'
           #print fname
           pygame.image.save(image,fname)

     except OSError:
        pass

     if fc == 0:
        catSurfaceObj = image
        windowSurfaceObj.blit(catSurfaceObj,(0,0))
     strim = pygame.image.tostring(image,"RGB",1)
     t3=time.time()
     if vtime < t3:
        if use_RPiGPIO == 1:
           GPIO.output(N_OP,GPIO.LOW)
           GPIO.output(S_OP,GPIO.LOW)
        keys2 ("N",18,6,b3x+42,b3y+38,1)
        keys2 ("S",18,6,b3x+42,b3y+102,1)
     if htime < t3:
        if use_RPiGPIO == 1:
           GPIO.output(E_OP,GPIO.LOW)
           GPIO.output(W_OP,GPIO.LOW)
        keys2 ("W",18,6,b3x+8,b3y+70,1)
        keys2 ("E",18,6,b3x+75,b3y+70,1)
     if ptime2 < t3 and photo == 1 and camera == 1:
        pcount2 = pcount2 - 1
        pygame.draw.rect(windowSurfaceObj,dgryColor,Rect(b2x+152,b2y+48, 32, 14))#
        keys2 (str((pcount + 1) - pcount2),12,photo,b2x+153,b2y+48,0)
        if pcount2 == 0:
           photo = 0
           camera = 0
           achange = 0
           button2 (b2x+129,b2y+33,63,31,0,photo)#
           keys2 ("PHOTO",12,photo,b2x+137,b2y+35,0)
           if use_RPiGPIO == 1:
              GPIO.output(C_OP,GPIO.LOW)
        else:
           camera = 0
           ptime2 = time.time() + pwait
           pygame.draw.rect(windowSurfaceObj,dgryColor,Rect(b2x+152,b2y+48, 32, 14))#
           GPIO.output(C_OP,GPIO.LOW)
     if ptime2 < t3 and photo == 1 and camera == 0:
        camera = 1
        ptime2 = time.time() + ptime
        keys2 (str((pcount + 1) - pcount2),12,photo,b2x+153,b2y+48,0)
        if use_RPiGPIO == 1:
           GPIO.output(C_OP,GPIO.HIGH)

  if camera_connected == 1 and use_Pi_Cam == 1:
     rpistopNS = 0
     rpistopEW = 0
     mkey = 0
     while os.path.exists('/run/shm/test.jpg') == False and mkey == 0:
        t2=time.time()
        if vtime < t2 and rpistopNS == 0:
           if use_RPiGPIO == 1:
              GPIO.output(N_OP,GPIO.LOW)
              GPIO.output(S_OP,GPIO.LOW)
           keys2 ("N",18,6,b3x+42,b3y+38,1)
           keys2 ("S",18,6,b3x+42,b3y+102,1)
           rpistopNS = 1
        if htime < t2 and rpistopEW == 0:
           if use_RPiGPIO == 1:
              GPIO.output(E_OP,GPIO.LOW)
              GPIO.output(W_OP,GPIO.LOW)
           keys2 ("W",18,6,b3x+8,b3y+70,1)
           keys2 ("E",18,6,b3x+75,b3y+70,1)
           rpistopEW = 1
        if rpiss > 900000:
           for event in pygame.event.get():
              if event.type == MOUSEBUTTONUP:
                 mkey = 1
     if mkey == 0:
        imagefile = ('/run/shm/test.jpg')
     if mkey == 1:
        imagefile = ('/run/shm/oldtest.jpg')
        pygame.event.post(event)
     t3=time.time()
     if vtime < t3 and rpistopNS == 0:
        if use_RPiGPIO == 1:
           GPIO.output(N_OP,GPIO.LOW)
           GPIO.output(S_OP,GPIO.LOW)
        keys2 ("N",18,6,b3x+42,b3y+38,1)
        keys2 ("S",18,6,b3x+42,b3y+102,1)
     if htime < t3 and rpistopEW == 0:
        if use_RPiGPIO == 1:
           GPIO.output(E_OP,GPIO.LOW)
           GPIO.output(W_OP,GPIO.LOW)
        keys2 ("W",18,6,b3x+8,b3y+70,1)
        keys2 ("E",18,6,b3x+75,b3y+70,1)
     if ptime2 < t3 and photo == 1 and camera == 1:
        pcount2 = pcount2 - 1
        pygame.draw.rect(windowSurfaceObj,dgryColor,Rect(b2x+152,b2y+48, 32, 14))#
        keys2 (str((pcount + 1) - pcount2),12,photo,b2x+153,b2y+48,0)
        if pcount2 == 0:
           photo = 0
           camera = 0
           achange = 0
           button2 (b2x+129,b2y+33,63,31,0,photo)#
           keys2 ("PHOTO",12,photo,b2x+137,b2y+35,0)#
           if use_RPiGPIO == 1:
              GPIO.output(C_OP,GPIO.LOW)
        else:
           camera = 0
           ptime2 = time.time() + pwait
           pygame.draw.rect(windowSurfaceObj,dgryColor,Rect(b2x+152,b2y+48, 32, 14))#
           GPIO.output(C_OP,GPIO.LOW)
     if ptime2 < t3 and photo == 1 and camera == 0:
        camera = 1
        ptime2 = time.time() + ptime
        keys2 (str((pcount + 1) - pcount2),12,photo,b2x+153,b2y+48,0)
        if use_RPiGPIO == 1:
           GPIO.output(C_OP,GPIO.HIGH)

     image = pygame.image.load(imagefile)
     try:
        if shift == 1:
           filno = filno + 1
           now = datetime.datetime.now()
           timestamp = now.strftime("%y%m%d%H%M%S")
           fname = '/home/pi/pic' + str(timestamp)+ "_" + str(filno) + '.jpg'
           #print fname
           shutil.copy('/run/shm/test.jpg',fname)

        os.rename('/run/shm/test.jpg','/run/shm/oldtest.jpg')
        os.remove('/run/shm/test.jpg')
     except OSError:
        pass

     catSurfaceObj = image
     windowSurfaceObj.blit(catSurfaceObj,(0,0))
     strim = pygame.image.tostring(image,"RGB",1)

  if camera_connected == 0:
     image = pygame.image.fromstring(imu,(width,height),"RGB",1) 
     catSurfaceObj = image
     windowSurfaceObj.blit(catSurfaceObj,(0,0))
     strim = imu
     t2=time.time()
     if vtime < t2:
        if use_RPiGPIO == 1:
           GPIO.output(N_OP,GPIO.LOW)
           GPIO.output(S_OP,GPIO.LOW)
        keys2 ("N",18,6,b3x+42,b3y+38,1)
        keys2 ("S",18,6,b3x+42,b3y+102,1)
     if htime < t2:
        if use_RPiGPIO == 1:
           GPIO.output(E_OP,GPIO.LOW)
           GPIO.output(W_OP,GPIO.LOW)
        keys2 ("W",18,6,b3x+8,b3y+70,1)
        keys2 ("E",18,6,b3x+75,b3y+70,1)

# crop picture
  strt = width * 3 * (((height/2)-(crop/2)) - offset4)
  imb = ""
  c = 0
  stas = strt + ((((width/2) - (crop/2))+offset3) * 3)
  while c < crop:
     imb = imb + strim[stas : stas + (crop*3)]
     stas +=(width*3)
     c +=1

# initialise arrays
  ars = {}
  art = {}
  arp = {}
  mx = []

# calculate min nd max values
  arp = [0] * 260

  if frames < 4:
     imc = imb[frames-1:crop*crop*3:3]
     mx = [ord(i) for i in imc]
     xcounter = frames-1
     ste = (int(crop/50)+1)
     while xcounter < (crop*crop):
        ima = ord(imc[xcounter:xcounter+1])
        arp[ima+1] = arp[ima+1]+1
        xcounter +=ste

  else:
     xcounter = 0
     ste = 1
     while xcounter < (crop*crop*3):
        imrc = ord(imb[xcounter:xcounter+1])
        imgc = ord(imb[xcounter+1:xcounter+2])
        imbc = ord(imb[xcounter+2:xcounter+3])
        ima = (imrc + imgc + imbc )/3
        mx.append(ima)
        arp[ima+1] = arp[ima+1]+1
        xcounter += 3

  count = 1
  mintot = 0
  maxtot = 0
  totm = 0
  while count <= 258:
     val = arp[count]
     totm = totm + val                                                                                                                   
     if totm < ((crop * crop)/ste) * 0.02:
        mintot = count
     if totm < ((crop * crop)/ste) - 11 :
        maxtot = count
     count +=1
  if auto_t == 0:
     pcont = maxtot - Sens
  else:
     pcont = maxtot - ((maxtot-mintot)/a_thr_scale)
     Sens = ((maxtot-mintot)/a_thr_scale)
     if Sens <= a_thr_limit:
        Sens = (maxtot -mintot)+1 
        pcont = maxtot - Sens

  if graph == 1:
     if Display == 0:
        pox = 585
     else:
        pox = width + 65
     poy = 20
     pov = 50
     pygame.draw.line(windowSurfaceObj, greyColor, (pox-1,poy-1),(pox+pov+1,poy-1))
     pygame.draw.line(windowSurfaceObj, greyColor, (pox+pov+1,poy-1),(pox+pov+1,poy+257))
     pygame.draw.line(windowSurfaceObj, greyColor, (pox+pov+1,poy+257),(pox-1,poy+257))
     pygame.draw.line(windowSurfaceObj, greyColor, (pox-1,poy+257),(pox-1,poy-1))
     count = 0
     img = ""
     while count < 256:
        if count != maxtot and count != mintot and count != pcont:
           val = arp[count+1]
           val2 = int((30 * math.log10(val + 10))-29)
           if val2 > 49:
              val2 = 49
           if val2 < 1:
              val2 = 1
           if val2 <= 1:
              byte = blankline[0:pov*3]
           if val2 > 1:
              if frames == 1:
                 byte = redline[0:(val2)*3]
              if frames == 2:
                 byte = greline[0:(val2)*3]
              if frames == 3:
                 byte = bluline[0:(val2)*3]
              if frames == 4:
                 byte = gryline[0:(val2)*3]
              byte = byte + blankline[0:(pov-val2)*3]
           img = img + byte
        if count == maxtot or count == mintot:
           mcount = 0                                                                                                                                         
           while mcount < pov:
              img = img + " zz"
              mcount +=1
        if count == pcont :
           mcount = 0
           while mcount < pov:
              img = img + "zz "
              mcount +=1
        count +=1
     if len(img) > (pov*256*3):
        img = img[0:(pov*256*3)]
     imageg = pygame.image.fromstring(img,(pov,256),"RGB",1) 
     catSurfaceObj = imageg
     windowSurfaceObj.blit(catSurfaceObj,(pox,poy))

  pic =""   
  counter = 0
  ttot = 0
  while counter < crop*crop :
     ima = mx[counter]
     if ima < pcont or pcont < mintot:
        mx[counter] = 0
        if thres == 1:
           pic = pic + imb[counter*3:(counter*3)+3]
     else:
        mx[counter] = 1
        ttot +=1
        if thres == 1:
           if frames == 1:
              pic = pic + chr(255)+chr(0)+chr(0)
           if frames == 2:
              pic = pic + chr(0)+chr(255)+chr(0)
           if frames == 3:
              pic = pic + chr(0)+chr(0)+chr(255)
           if frames == 4:
              pic = pic + chr(255)+chr(255)+chr(0)
     counter +=1
  if ttot < 1:
     ttot = 1
  if thres == 1:
     imagep = pygame.image.fromstring(pic,(crop,crop),"RGB",1) 
     catSurfaceObj = imagep
     windowSurfaceObj.blit(catSurfaceObj,((width/2)-(crop /2)+offset3,(height/2)-(crop /2)+offset4))

# calculate corrections
  if auto_win == 1:
     lcounter = 0
     while lcounter < crop:
        loc = lcounter * crop
        ars[lcounter]= sum(mx[loc:loc+crop])
        art[lcounter]= sum(mx[lcounter:crop*crop:crop])
        lcounter +=1

  if ttot > 1:
     acorrect = 0
     ltot = 0
     while ltot < ttot/2:
        loc = acorrect * crop
        if auto_win == 1:
           ltot = ltot + ars[acorrect]
        else:
           ltot = ltot + sum(mx[loc:loc+crop])
        acorrect +=1

     bcorrect = 0
     ctot = 0
     while ctot < ttot/2:
        if auto_win == 1:
           ctot = ctot + art[bcorrect]
        else:
           ctot = ctot + sum(mx[bcorrect:crop*crop:crop])
        bcorrect +=1

     if acorrect < crop/2:
        acorrect = 100 * (0-(crop/2 - acorrect))
     else:
        acorrect = 100 * (acorrect - crop/2)
     if bcorrect < crop/2:
        bcorrect = 100 * (0-(crop/2 - bcorrect))
     else:
        bcorrect = 100 * (bcorrect - crop/2)

  if pcont == 0 or ttot == 1 :
     acorrect = 1
     bcorrect = 1

# auto_c
  if auto_c == 1:
     if ttot > 1:
        if offset3 > 0:
           offset3 = offset3 - 1
        if offset3 < 0:
           offset3 = offset3 + 1
        if offset4 > 0:
           offset4 = offset4 - 1
        if offset4 < 0:
           offset4 = offset4 + 1
        if offset3 == 0 and offset4 ==    0:
           auto_c = 0
           auto_win = 0
           keys2 ("cen",14,7,b3x+37,b3y+65,0)
           keys2 ("tre",14,7,b3x+40,b3y+77,0)
     else:
        auto_c = 0
        auto_win = 0
        keys2 ("cen",14,7,b3x+37,b3y+65,0)
        keys2 ("tre",14,7,b3x+40,b3y+77,0)


  if auto_win == 1:
     if ttot > 1 and ars[3]< 2 and ars[crop-2] < 2 and art[3] < 2 and art[crop-2] < 2:
        crop = crop - 1
        if crop < minwin:
           crop = minwin
     if ttot > 1 and (ars[2] > 1 or ars[crop-2] > 1 or art[2] > 1 or art[crop-2] > 1):
        crop = crop + 1
        if crop > maxwin :
           crop = maxwin
        if (width/2 + crop/2 + offset3 > width) or ((width/2) + offset3 - (crop /2) ) <=1 or ((height/2) + offset4 - (crop /2) ) >= height or ((height/2) - offset4 - (crop /2) ) <=1:
           crop = crop - 2
     if ttot == 1 and auto_wlos:
        crop = crop + 1
        if crop > maxwin:
           crop = maxwin
        if ((width/2) + offset3 + (crop /2) ) >= width:
           crop  = crop  - 1
        if ((width/2) + offset3 - (crop /2) ) <=1:
           crop  = crop  - 1
        if ((height/2) + offset4 + (crop /2) ) >= height:
           crop  = crop  - 1
        if ((height/2) + offset4 - (crop /2) ) <=1:
           crop  = crop  - 1

  return auto_win,Display,fc,filno,rpibr,camera,ptime2,ptime,pcount2,pcount,photo,auto_c,acorrect,bcorrect,ttot,maxtot,mxo,crop,Sens,offset5,offset6,offset3,offset4

def commands (nscale,escale,sscale,wscale,ewi,nsi,acorrect,bcorrect,mincor):
  dirn = 'n'
  nsscale = int(nscale)
  lcorrect = (acorrect * nsscale)/100
  if nsi != 0:
     dirn = 's'
     nsscale = int(sscale)
     lcorrect = (acorrect * nsscale)/100
  if lcorrect <=0:
     dirn = 's'
     nsscale = int(sscale)
     lcorrect = (acorrect * nsscale)/100
     if nsi != 0:
        dirn = 'n'
        nsscale = int(nscale)
        lcorrect = (acorrect * nsscale)/100
     lcorrect = 0-lcorrect
  if lcorrect > 9999:
     lcorrect = 9999
  if lcorrect < mincor :
     lcorrect = 0
  Vcorrect = lcorrect
  Vcorlen = len(str(Vcorrect))
  ime = 1
  Vcorr =""
  while ime <= (4-Vcorlen):
     Vcorr = Vcorr + '0'
     ime +=1
  Vcorrt =':Mg'+dirn+Vcorr+str(Vcorrect)

  dirn = 'e'
  ewscale = int(wscale)
  ccorrect = (bcorrect * ewscale)/100
  if ewi != 0:
     dirn = 'w'
     ewscale = int(escale)
     ccorrect = (bcorrect * ewscale)/100
  if ccorrect <=0:
     dirn = 'w'
     ewscale = int(escale)
     ccorrect = (bcorrect * ewscale)/100
     if ewi != 0:
        dirn = 'e'
        ewscale = int(wscale)
        ccorrect = (bcorrect * ewscale)/100
     ccorrect = 0-ccorrect
  if ccorrect > 9999:
     ccorrect = 9999
  if ccorrect < mincor :
     ccorrect = 0
  Hcorrect = ccorrect
  Hcorlen = len(str(Hcorrect))
  ime = 1
  Hcorr=""
  while ime <= (4-Hcorlen):
     Hcorr = Hcorr + '0'
     ime +=1
  Hcorrt =':Mg'+dirn+Hcorr+str(Hcorrect)
  return Vcorrt,Hcorrt,ewi,nsi

def lx200 (Vcorrt,Hcorrt):
  if serial_connected ==1:
     ser.write(bytes(Vcorrt.encode('ascii')))
     time.sleep(0.5)
     ser.write(bytes(Hcorrt.encode('ascii')))
  return

oldnscale = nscale
oldescale = escale
oldsscale = sscale
oldwscale = wscale
oldcrop  = crop 
oldauto_g = auto_g
oldInterval = Interval
oldlog = log
oldframes = frames
oldSens = Sens
oldgraph = graph
oldthres = thres
oldauto_i = auto_i
oldplot = plot
oldauto_win = auto_win
oldauto_t = auto_t
oldcrop_img = crop_img
oldrpibr = rpibr
oldrpico = rpico
oldrpiss = rpiss
oldrpiexno = rpiexno
oldrpiISO = rpiISO
oldrpiev = rpiev
oldcls = cls
oldave = ave
oldshift = shift
oldphoto = photo
oldptime = ptime
oldpcount = pcount

arv = {}
arh = {}
arp = {}
count = 0
crop  = crop 
oldnsi = nsi
oldewi = ewi
posx = (width/2)
posy = (height/2)
posxi = -1
posxj = 1
posyi = -1
posyj = 1
poss = 3
cycle = 0
wd = 20
hd = 20
pct = 1
pcu = 1


wz = 1
blankline = chr(3)+ chr(3)+ chr(3)
while wz < width:
  blankline = blankline + chr(3)+ chr(3)+ chr(3)
  wz +=1
wz = 1
redline = chr(128)+ chr(0)+ chr(0)
while wz < 50:
  redline = redline + chr(128)+ chr(0)+ chr(0)
  wz +=1
wz=1
bluline = chr(0)+ chr(0)+ chr(128)
while wz < 50:
  bluline = bluline + chr(0)+ chr(0)+ chr(128)
  wz +=1
wz=1
greline = chr(0)+ chr(64)+ chr(0)
while wz < 50:
  greline = greline + chr(0)+ chr(64)+ chr(0)
  wz +=1
wz=1
gryline = chr(128)+ chr(128)+ chr(128)
while wz < 50:
  gryline = gryline + chr(128)+ chr(128)+ chr(128)
  wz +=1
if serial_connected ==1:
  ser = serial.Serial('/dev/ttyACM0',9600)
start = time.time()
start2 = start
totvcor = 0
tothcor = 0
avevcor = 0
avehcor = 0
xcount = 0
xycle = 0
xvcor = {}
xhcor = {}
count = 1
ycount = 0
xvcor = [0] * 11
xhcor = [0] * 11
filno = 1

while cycle == 0:
  xycle += 1
  if camera_connected == 0:
     posxa =  random.randrange(posxi,posxj)
     posya =  random.randrange(posyi,posyj)
     posx = posx + posxa
     if posx > (width-50):
         posxi = -2
         posxj = 0
     if posx < 50:
         posxi = 0
         posxj = 2
     posy = posy + posya
     if posy > (height-50):
         posyi = -2
         posyj = 0
     if posy < 50:
         posyj = 2
         posyi = 0
     if (time.time() - start) >= Interval - 0.2 and serial_connected == 0:
        if auto_g == 1 and ((bcorrect/100) * escale) > mincor:
           posx = posx - (bcorrect/100)
           start = time.time()
        if auto_g == 1 and ((acorrect/100) * nscale) > mincor:   
           posy = posy - (acorrect/100)
           start = time.time()
        if auto_g == 1 and 0-((bcorrect/100) * escale) > mincor:
           posx = posx - (bcorrect/100)
           start = time.time()
        if auto_g == 1 and 0-((acorrect/100) * nscale) > mincor:   
           posy = posy - (acorrect/100)
           start = time.time()
     imu = demo(width,height,posx,posy,blankline,wd,hd)

  auto_win,Display,fc,filno,rpibr,camera,ptime2,ptime,pcount2,pcount,photo,auto_c,acorrect,bcorrect,ttot,maxtot,mxo,crop,Sens,offset5,offset6,offset3,offset4 = picture (auto_win,Display,fc,shift,filno,pwait,camera,ptime2,auto_c,width,height,crop ,offset3,offset4,offset5,offset6,frames,Sens,camera_connected,redline,greline,bluline,gryline,blankline,imu,mxo,auto_t,arp,crop_img,w,h,rpico,rpibr,rpit,rpiex,rpiISO,rpiss,vtime,htime,ptime,pcount2,pcount,photo,use_Pi_Cam,use_RPiGPIO)

  if (auto_g == 1 and ttot == 1 and cls == 1 and ymax == avemax):
     acorrect = avevcor
     bcorrect = avehcor


  if auto_g == 1 and ttot == 1:
     keys2 ("AutoGuide",16,2,b1x+22,b1y+39,0)
  if auto_g == 1 and ttot == 1 and cls == 1:
     keys2 ("CLS",12,2,b2x+132,b2y+170,0)
  if auto_g == 1 and ttot > 1:
     keys2 ("AutoGuide",16,1,b1x+22,b1y+39,0)
  if auto_g == 1 and ttot > 1 and cls == 1:
     keys2 ("CLS",12,1,b2x+132,b2y+170,0)

  Vcorrt,Hcorrt,ewi,nsi = commands (nscale,escale,sscale,wscale,ewi,nsi,acorrect,bcorrect,mincor)

  if xycle >= Interval:
     xycle = 0

     if auto_g == 1 :
        xcount +=1
        ycount +=1
        zcount = ycount
        if ycount > avemax:
           ymax = avemax
        if ymax == avemax:
           zcount = avemax
        if cls == 1 and zcount > avemax-1:
           keys2 ("CLS",12,1,b2x+132,b2y+170,0)
        if cls == 1 and zcount < avemax:
           keys2 ("CLS",12,2,b2x+132,b2y+170,0)
        if ave == 1 and zcount > avemax-1 and aveon == 1:
           keys2 ("AVE",12,1,b2x+164,b2y+170,0)
        if ave == 1 and zcount < avemax and aveon == 1:
           keys2 ("AVE",12,2,b2x+164,b2y+170,0)
        if ycount > avemax :
           ycount = 1

        if ttot > 1 :
           xvcor[ycount] = acorrect
           xhcor[ycount] = bcorrect
           totvcor = 0
           tothcor = 0
           count = 1
           while count <= zcount:
              totvcor = totvcor + xvcor[count]
              tothcor = tothcor + xhcor[count]
              count +=1
           avevcor = int(totvcor/zcount)
           avehcor = int(tothcor/zcount)

        if ttot > 1 and ave == 1 and aveon == 1 and acorrect > 0 and avevcor > 0 and ymax == avemax:
           acorrect = avevcor
        if ttot > 1 and ave == 1 and aveon == 1 and acorrect < 0 and avevcor < 0 and ymax == avemax:
           acorrect = avevcor
        if ttot > 1 and ave == 1 and aveon == 1 and bcorrect < 0 and avehcor < 0 and ymax == avemax:
           bcorrect = avehcor
        if ttot > 1 and ave == 1 and aveon == 1 and bcorrect > 0 and avehcor > 0 and ymax == avemax:
           bcorrect = avehcor

           Vcorrt,Hcorrt,ewi,nsi = commands (nscale,escale,sscale,wscale,ewi,nsi,acorrect,bcorrect,mincor)

        vdir = Vcorrt[3:4]
        hdir = Hcorrt[3:4]
        vcor = int(Vcorrt[4:8])
        hcor = int(Hcorrt[4:8])   
        time1 = time.time()
        vtime = ((time1 * 1000) + vcor)/1000
        htime = ((time1 * 1000) + hcor)/1000
        start = time.time()
        if vdir == "n" and vcor > 0:
           if use_RPiGPIO == 1:
              GPIO.output(N_OP,GPIO.HIGH)
              GPIO.output(S_OP,GPIO.LOW)
           keys2 ("N",18,1,b3x+42,b3y+38,1)
           keys2 ("S",18,6,b3x+42,b3y+102,1)
        if hdir == "e" and hcor > 0:
           if use_RPiGPIO == 1:
              GPIO.output(E_OP,GPIO.HIGH)
              GPIO.output(W_OP,GPIO.LOW)
           keys2 ("E",18,1,b3x+75,b3y+70,1)
           keys2 ("W",18,6,b3x+8,b3y+70,1)
        if vdir == "s" and vcor > 0:
           if use_RPiGPIO == 1:
              GPIO.output(S_OP,GPIO.HIGH)
              GPIO.output(N_OP,GPIO.LOW)
           keys2 ("S",18,1,b3x+42,b3y+102,1)
           keys2 ("N",18,6,b3x+42,b3y+38,1)
        if hdir == "w" and hcor > 0:
           if use_RPiGPIO == 1:
              GPIO.output(W_OP,GPIO.HIGH)
              GPIO.output(E_OP,GPIO.LOW)
           keys2 ("W",18,1,b3x+8,b3y+70,1)
           keys2 ("E",18,6,b3x+75,b3y+70,1)
        if serial_connected ==1:
           ser.write(bytes(Vcorrt.encode('ascii')))
           time.sleep(0.2)
           ser.write(bytes(Hcorrt.encode('ascii')))
           time.sleep(0.2)
        if log == 1:
           now = datetime.datetime.now()
           timestamp = now.strftime("%y/%m/%d-%H:%M:%S")
           timp = str(timestamp) + "," + Vcorrt + "," + Hcorrt + "," + str(acorrect) + "," + str(bcorrect) + "\n"
           #filedata
           file = open(logfile, "a")
           file.write(timp)
           file.close()
        if camera_connected == 0:
           posx = posx - (bcorrect/100)
           posy = posy - (acorrect/100)

     if auto_i == 1 : #and Intervals < 10:
        acor = int(Vcorrt[4:8])
        bcor = int(Hcorrt[4:8])
        if acor >= bcor and acor > 0:
           Interval = (acor/1000)+2
           if use_Pi_Cam == 1 and rpiex == "off":
              Interval = Interval + ((rpiss/1000000)*4)
        if bcor > acor and bcor > 0:
           Interval = (bcor/1000)+2
           if use_Pi_Cam == 1 and rpiex == "off":
              Interval = Interval + ((rpiss/1000000)*4)
        if use_Pi_Cam == 0 and camera_connected == 1:
           Interval = Interval * 3
     else:
        Interval = Intervals

# Display
  if plot == 1:
     if Display == 0:
        pox = 530
     else:
        pox = width + 10
     poy = 20
     pov = 50
     pol = 256
     pygame.draw.line(windowSurfaceObj, greyColor, (pox-1,poy-1),(pox+pov+1,poy-1))
     pygame.draw.line(windowSurfaceObj, greyColor, (pox+pov+1,poy-1),(pox+pov+1,poy+pol+1))
     pygame.draw.line(windowSurfaceObj, greyColor, (pox+pov+1,poy+pol+1),(pox-1,poy+pol+1))
     pygame.draw.line(windowSurfaceObj, greyColor, (pox-1,poy+pol+1),(pox-1,poy-1))
     limg +=1
     val2 = pov/2
     val3 = pov/2
     if acorrect > 0:
        val2 = (pov/2) + int(math.sqrt(acorrect))/4
     if bcorrect > 0:
        val3 = (pov/2) + int(math.sqrt(bcorrect))/4
     if acorrect < 0:
        val2 = (pov/2) - int(math.sqrt(0-acorrect))/4
     if bcorrect < 0:
        val3 = (pov/2) - int(math.sqrt(0-bcorrect))/4
     if val2 < val3:
        rimg = blankline[0:(val2)*3]
        rimg = rimg + chr(255)+ chr(0)+ chr(0)
        rimg = rimg + blankline[(val2)*3:(val3)*3]
        rimg = rimg + chr(0)+ chr(255)+ chr(0)
        rimg = rimg + blankline
        pimg = pimg + rimg[0:pov*3]
     else:
        rimg = blankline[0:(val3)*3]
        rimg = rimg + chr(0)+ chr(255)+ chr(0)
        rimg = rimg + blankline[(val3)*3:(val2)*3]
        rimg = rimg + chr(255)+ chr(0)+ chr(0)
        rimg = rimg + blankline
        pimg = pimg + rimg[0:pov*3]
     if limg > pol:
        yt = (limg-pol)*pov*3
        yu = limg*pov*3
        pimg = pimg[yt:yu]
        limg = pol
     imageg = pygame.image.fromstring(pimg,(pov,limg),"RGB",1) 
     catSurfaceObj = imageg
     windowSurfaceObj.blit(catSurfaceObj,(pox,poy))

  if change == 1 or achange == 1:
     if photo == 0 and photoon == 1:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+152,b2y+48, 32, 12))
     if oldphoto != photo and photoon == 1:
        keys2 ("PHOTO",12,photo,b2x+137,b2y+35,0)
     if oldcls != cls and auto_g == 1:
        button2 (b2x+129,b2y+161,31,31,0,cls)
        keys2 ("CLS",12,cls,b2x+132,b2y+170,0)
     if oldshift != shift:
        button2 (b2x+161,b2y+161,31,31,0,shift)
        keys2 ("con",14,shift,b2x+164,b2y+164,0)
        keys2 ("cap",14,shift,b2x+164,b2y+174,0)
     if oldauto_win != auto_win:
        button2 (b1x+65,b1y+65,63,31,0,auto_win)
        keys2 ("A-Win",16,auto_win,b1x+75,b1y+71,0)
     if oldauto_i != auto_i:
        button2 (b1x+65,b1y+129,63,31,0,auto_i)
        keys2 ("A-Int",16,auto_i,b1x+76,b1y+136,0)
     if oldnsi != nsi:
        button2 (b1x+65,b1y+161,31,31,0,nsi)
        keys2 ("NSi",14,nsi,b1x+68,b1y+169,0)
     if oldewi != ewi:   
        button2 (b1x+97,b1y+161,31,31,0,ewi)
        keys2 ("EWi",14,ewi,b1x+100,b1y+169,0)
     if oldauto_t != auto_t:
        button2 (b1x+65,b1y+97,63,31,0,auto_t)
        keys2 ("A-Thr",16,auto_t,b1x+76,b1y+103,0)
     if oldauto_g != auto_g:
        button2 (b1x+1,b1y+33,127,31,0,auto_g)
        keys2 ("AutoGuide",16,auto_g,b1x+22,b1y+39,0)
     if oldlog != log :
        button2 (b1x+1,b1y+97,63,31,0,log)
        keys2 ("Log",16,log,b1x+15,b1y+103,0)
     if oldgraph != graph :
        button2 (b1x+1,b1y+129,63,31,0,graph)
        keys2 ("Graph",16,graph,b1x+11,b1y+135,0)
     if oldplot != plot :
        button2 (b1x+1,b1y+65,63,31,0,plot)
        keys2 ("Plot",16,plot,b1x+17,b1y+71,0)
     if oldthres != thres :
        button2 (b1x+1,b1y+161,63,31,0,thres)
        keys2 ("Thres",16,thres,b1x+11,b1y+167,0)
     if oldptime != ptime and photoon == 1:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+147,b2y+78, 26, 16))
        keys2 (str(ptime),14,3,b2x+147,b2y + 78,0)
     if oldpcount != pcount and photoon == 1:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+153,b2y+110, 26, 16))
        keys2 (str(pcount),14,3,b2x+153,b2y + 110,0)
     if oldnscale != nscale:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+84,b2y+47, 26, 16))
        keys2 (str(int(nscale)),14,3,b2x+84,b2y+47,0)
        mincor = (nscale + sscale + wscale + escale)/4
     if oldsscale != sscale:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+84,b2y+79, 26, 16))
        keys2 (str(int(sscale)),14,3,b2x+84,b2y+79,0)
        mincor = (nscale + sscale + wscale + escale)/4
     if oldescale != escale:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+84,b2y+111, 26, 16))
        keys2 (str(int(escale)),14,3,b2x+84,b2y+111,0)
        mincor = (nscale + sscale + wscale + escale)/4
     if oldwscale != wscale:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+84,b2y+143, 26, 16))
        keys2 (str(int(wscale)),14,3,b2x+84,b2y+143,0)
        mincor = (nscale + sscale + wscale + escale)/4
     if oldSens != Sens:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b1x+148,b1y+111, 26, 16))
        keys2 (str(Sens),14,3,b1x+148,b1y+111,0)
     if oldInterval != Interval:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b1x+148,b1y+143, 26, 16))
        keys2 (str(Interval),14,3,b1x+152,b1y+143,0)
     if oldcrop_img != crop_img:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b1x+148,b1y+175, 26, 16))
        keys2 (str(crop_img),14,3,b1x+155,b1y+175,0)
     if oldframes != frames:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b1x+150,b1y+47, 26, 16))
        msg = rgb[frames]
        keys2 (msg,16,frames + 2,b1x+153,b1y+47,0)
     if oldcrop  != crop :
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b1x+146,b1y+79, 26, 16))
        keys2 (str(crop),14,3,b1x+148,b1y+79,0)
     if oldrpibr != rpibr :
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+22,b2y + 47, 26, 16))
        keys2 (str(rpibr),14,3,b2x+22,b2y + 47,0)
     if oldrpico != rpico:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+19,b2y + 79, 26, 16))
        keys2 (str(rpico),14,3,b2x+19,b2y + 79,0)
     if oldrpiss != rpiss:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+17,b2y + 111, 26, 16))
        keys2 (str(int(rpiss/1000)),13,3,b2x+17,b2y + 111,0)
     if oldrpiexno != rpiexno:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+14,b2y + 143, 38, 16))
        keys2 (rpimodesa[rpiexno],14,3,b2x+14,b2y + 143,0)
        if rpimodes[rpiexno] != "off":
           pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+14,b2y+ 111, 33, 16))
           keys2 ('off',14,3,b2x+22,b2y + 111,0)
        else:
           pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+19,b2y + 111, 26, 16))
           keys2 (str(rpiss/1000),14,3,b2x+20,b2y + 111,0)
     if oldrpiISO != rpiISO:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+19,b2y + 175, 26, 16))
        keys2 (str(rpiISO),14,3,b2x+19,b2y + 175,0)
        if rpiISO == 0:
           pygame.draw.rect(windowSurfaceObj,greyColor,Rect(b2x+19,b2y + 175, 26, 16))
           keys2 ('off',14,3,b2x+22,b2y + 175,0)
     if oldrpiev != rpiev:
        pygame.draw.rect(windowSurfaceObj,greyColor,Rect(455,170, 31, 31))
        keys2 (str(rpiev),14,3,455,170,0)
        if rpiev == 0:
           pygame.draw.rect(windowSurfaceObj,blackColor,Rect(455,170, 31, 31))
           keys2 ('off',14,3,455,170,0)

  w2 = width/2 + offset3
  h2 = height/2 + offset4
  c1 = crop /2
  c2 = crop /2

  if auto_g == 0:
     pygame.draw.line(windowSurfaceObj, purpleColor, ((w2+(bcorrect/100)-5),(h2-(acorrect/100)-5)),((w2+(bcorrect/100)+5),(h2-(acorrect/100)+5)))
     pygame.draw.line(windowSurfaceObj, purpleColor, ((w2+(bcorrect/100)+5),(h2-(acorrect/100)-5)),((w2+(bcorrect/100)-5),(h2-(acorrect/100)+5)))
  if auto_g == 1 and ttot > 1:
     pygame.draw.line(windowSurfaceObj, greenColor, ((w2 +(bcorrect/100)-5),(h2-(acorrect/100)-5)),((w2+(bcorrect/100)+5),(h2-(acorrect/100)+5)))
     pygame.draw.line(windowSurfaceObj, greenColor, ((w2 +(bcorrect/100)+5),(h2-(acorrect/100)-5)),((w2+(bcorrect/100)-5),(h2-(acorrect/100)+5)))
  if auto_g == 1 and ttot == 1:
     pygame.draw.line(windowSurfaceObj, yellowColor, ((w2 +(bcorrect/100)-5),(h2-(acorrect/100)-5)),((w2+(bcorrect/100)+5),(h2-(acorrect/100)+5)))
     pygame.draw.line(windowSurfaceObj, yellowColor, ((w2 +(bcorrect/100)+5),(h2-(acorrect/100)-5)),((w2+(bcorrect/100)-5),(h2-(acorrect/100)+5)))

  if fc == 0 or use_Pi_Cam == 1 or camera_connected == 0 or thres == 1:
     pygame.draw.line(windowSurfaceObj, redColor, (w2-c1,h2-c2),(w2+c1,h2-c2))
     pygame.draw.line(windowSurfaceObj, redColor, (w2+c1,h2-c2),(w2+c1,h2+c2))
     pygame.draw.line(windowSurfaceObj, redColor, (w2+c1,h2+c2),(w2-c1,h2+c2))
     pygame.draw.line(windowSurfaceObj, redColor, (w2-c1,h2+c2),(w2-c1,h2-c2))
     pygame.draw.line(windowSurfaceObj, redColor, (w2-4,h2),(w2+4,h2))
     pygame.draw.line(windowSurfaceObj, redColor, (w2,h2-4),(w2,h2+4))
     if auto_g == 0:
        keys2 (Vcorrt,16,9,0,0,0)
        keys2 (Hcorrt,16,9,width - 76,0,0)
     if auto_g == 1 and ttot > 1:
        keys2 (Vcorrt,16,1,0,0,0)
        keys2 (Hcorrt,16,1,width - 76,0,0)
     if auto_g == 1 and ttot == 1:
        keys2 (Vcorrt,16,2,0,0,0)
        keys2 (Hcorrt,16,2,width - 76,0,0)

  pygame.display.update()

  oldnscale = nscale
  oldescale = escale
  oldsscale = sscale
  oldwscale = wscale
  oldcrop  = crop 
  oldauto_g = auto_g
  oldtrack = track
  oldInterval = Interval
  oldlog = log
  oldframes = frames
  oldSens = Sens
  oldgraph = graph
  oldnsi = nsi
  oldewi = ewi
  oldthres = thres
  oldauto_i = auto_i
  oldplot = plot
  oldauto_win = auto_win
  oldauto_t = auto_t
  oldcrop_img = crop_img
  oldrpibr = rpibr
  oldrpico = rpico
  oldrpiss = rpiss
  oldrpiexno = rpiexno
  oldrpiISO = rpiISO
  oldrpiev = rpiev
  oldcls = cls
  oldave = ave
  oldshift = shift
  oldphoto = photo
  oldptime = ptime
  oldpcount = pcount
  change = 0

# read mouse or keyboard

  for event in pygame.event.get():
      if event.type == QUIT:
         if use_Pi_Cam == 1 and camera_connected == 1:
            os.killpg(p.pid, signal.SIGTERM)
         pygame.quit()
         sys.exit()

      elif event.type == MOUSEBUTTONUP or event.type == KEYDOWN:
         start = time.time()
         change = 1
         xcount = 0
         ycount = 0
         ymax = 0
         totvcor = 0
         tothcor = 0
         count = 1
         xvcor = [0] * 11
         xhcor = [0] * 11
         keys2 ("N",18,6,b3x+42,b3y+38,1)
         keys2 ("S",18,6,b3x+42,b3y+102,1)
         if use_RPiGPIO == 1:
            GPIO.output(N_OP,GPIO.LOW)
            GPIO.output(S_OP,GPIO.LOW)
         keys2 ("W",18,6,b3x+8,b3y+70,1)
         keys2 ("E",18,6,b3x+75,b3y+70,1)
         if use_RPiGPIO == 1:
            GPIO.output(E_OP,GPIO.LOW)
            GPIO.output(W_OP,GPIO.LOW)
         restart = 0
         z = 0
         kz = 0
         if event.type == KEYDOWN:
            kz = event.key
            #print kz
         if event.type == MOUSEBUTTONUP:
            mousex,mousey = event.pos
            if Display != 0:
               x = mousex/32
               if mousey > height:
                  y = (mousey-height)/32
                  z = (10*x)+y
            else:
               if mousex > width:
                  if mousey < 160:
                     x = int((mousex - width)/32)
                     y = int(mousey/32)+1
                     z = (10*x)+y
                  if mousey > 160 and mousey < 320:
                     x = int((mousex - width)/32)+6
                     y = int((mousey-160)/32)+1
                     z = (10*x)+y
                  if mousey > 320:
                     x = int((mousex - width)/32)+12
                     y = int((mousey-320)/32)+1
                     z = (10*x)+y
            if ((Display == 0 and mousex > 585) or (Display != 0 and mousex > width + 60)) and ((Display == 0 and mousex < 635) or (Display != 0 and mousex < width +110)) and mousey > 20 and mousey < 276 and graph == 1:
               if((Display == 0 and mousex > 585) or (Display != 0 and mousex > width + 60))and ((Display == 0 and mousex < 635) or (Display != 0 and mousex < width +110)) and mousey > (256 - maxtot) and mousey < 276 :
                  level = 276 - mousey
                  Sens = maxtot - level
                  if Sens < 1:
                     Sens = 1
            else:
               if mousex < width and mousey < height :     
                  start = time.time()
                  xcount = 0
                  ycount = 0
                  totvcor = 0
                  tothcor = 0
                  count = 1
                  while count <= 10:
                     xvcor[count] = 0
                     xhcor[count] = 0
                     count +=1
                  offset3o = offset3
                  offset4o = offset4
                  offset3 = 0-((width/2)- mousex)
                  offset4 = 0-((height/2) - mousey)
                  if ((width/2) + offset3 + (crop /2) ) >= width or ((width/2) + offset3 - (crop /2) ) <=1:
                     offset3 = offset3o 
                     offset4 = offset4o 
                  if ((height/2) + offset4 + (crop /2) ) >= height or ((height/2) + offset4 - (crop /2) ) <=1:
                     offset3 = offset3o 
                     offset4 = offset4o

         if z == 115 or kz == 304 or kz == 303:
            shift = shift + 1
            if shift > 1:
               shift = 0
         if z > 60 and z < 76 and use_Pi_Cam == 1 and camera_connected == 1:
            os.killpg(p.pid, signal.SIGTERM)
         if z == 114:
            keys2 ("scr",14,1,b2x+165,b2y+130,1)
            keys2 ("cap",14,1,b2x+165,b2y+141,1)
            pygame.image.save(windowSurfaceObj,'scr_pic' + str(pct)+'.jpg')
            keys2 ("scr",14,6,b2x+165,b2y+130,1)
            keys2 ("cap",14,6,b2x+165,b2y+141,1)
            pct +=1
         if z == 104 and use_Pi_Cam == 1 and camera_connected == 1:
            os.killpg(p.pid, signal.SIGTERM)
            keys2 ("pic",14,1,b2x+133,b2y+130,1)
            keys2 ("cap",14,1,b2x+133,b2y+141,1)
            rpistr = "raspistill -o cap_pic" + str(pcu) + ".jpg -co " + str(rpico) + " -br " + str(rpibr)
            if rpiex != 'off':
               rpistr = rpistr + " -t 800 -ex " + rpiex
            else:
               rpistr = rpistr + " -t 10 -ss " + str(rpiss)
            if rpiISO > 0:
               rpistr = rpistr + " -ISO " + str(rpiISO)
            if rpiev != 0:
               rpistr = rpistr + " -ev " + str(rpiev)
            rpistr = rpistr + " -n -sa " + str(rpisa)
            path = rpistr + ' -w 2592 -h 1944'
            os.system (path)
            keys2 ("pic",14,6,b2x+133,b2y+130,0)
            keys2 ("cap",14,6,b2x+133,b2y+141,0)
            pcu +=1
            restart = 1
         if (z == 151 or z == 161 or z == 171) and crop_img == Image_window:
            offset4 = offset4 - 1
            if (((height/2) + offset4 + (crop /2) ) >= height) or (((height/2) + offset4 - (crop /2) ) <=1):
               offset4 = offset4 + 1            
         if ( z == 153 or z == 163 or z == 173) and crop_img == Image_window:
            offset4 = offset4 + 1
            if (((height/2) + offset4 + (crop /2) ) >= height) or (((height/2) + offset4 - (crop /2) ) <=1):
               offset4 = offset4 - 1 
         if (z == 172 or z == 171 or z == 173) and crop_img == Image_window:
            offset3 = offset3 + 1
            if (((width/2) + offset3 + (crop /2) ) >= width) or (((width/2) + offset3 - (crop /2) ) <=1):
               offset3 = offset3 - 1
         if (z == 151 or z == 153 or z == 152)  and crop_img == Image_window:
            offset3 = offset3 - 1
            if (((width/2) + offset3 + (crop /2) ) >= width) or (((width/2) + offset3 - (crop /2) ) <=1):
               offset3 = offset3 + 1

         if (z == 151 or z == 161 or z == 171) and crop_img > Image_window:
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
               restart = 1
            offset6 = offset6 - 5
            if offset6 > 0 and offset6 >= (h/2)-(height/2):
               offset6 = offset6 + 5
            if offset6 < 0 and offset6 <= 0-((h/2)-(height/2)):
               offset6 = offset6 + 5            
         if ( z == 153 or z == 163 or z == 173) and crop_img > Image_window:
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
               restart = 1
            offset6 = offset6 + 5
            if offset6 > 0 and offset6 >= (h/2)-(height/2):
               offset6 = offset6 - 5
            if offset6 < 0 and offset6 <= 0-((h/2)-(height/2)):
               offset6 = offset6 - 5 
         if (z == 172 or z == 171 or z == 173) and crop_img > Image_window:
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
               restart = 1
            offset5 = offset5 + 5
            if offset5 > 0 and offset5 >= (w/2)-(width/2):
               offset5 = offset5 - 5
            if offset5 < 0 and offset5 <= 0-((w/2)-(width/2)):
               offset5 = offset5 - 5
         if (z == 151 or z == 153 or z == 152) and crop_img > Image_window:
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
               restart = 1
            offset5 = offset5 - 5
            if offset5 > 0 and offset5 >= (w/2)-(width/2):
               offset5 = offset5 + 5
            if offset5 < 0 and offset5 <= 0-((w/2)-(width/2)):
               offset5 = offset5 + 5
         if z == 162 and crop_img > 0:
            offset6a = offset6
            offtry = 0
            while offtry < 10 and (offset6a == offset6):
               offset4a = int(offset4 * (1-(offtry/10)))
               offset6a = offset6 + offset4a
               if (offset6a > 0 and offset6a >= (h/2)-(height/2)) or (offset6a < 0 and offset6a <= 0-((h/2)-(height/2))):
                  offset6a = offset6a - offset4a
               offtry +=1
            if offtry < 10:
               offset6 = offset6a
               offset4 = offset4 - offset4a
            offset5a = offset5
            offtry = 1
            while offtry < 10 and (offset5a == offset5):
               offset3a = int(offset3/offtry)
               offset5a = offset5 + offset3a
               if (offset5a > 0 and offset5a >= (w/2)-(width/2)) or (offset5a < 0 and offset5a <= 0-((w/2)-(width/2))):
                  offset5a = offset5a - offset3a
               offtry +=1
            if offtry < 10:
               offset5 = offset5a
               offset3 = offset3 - offset3a
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
               restart = 1
         if z == 54:
            Interval = Intervals
            Interval = Interval +1
            Intervals = Interval
         if z == 44:
            Interval = Intervals
            Interval = Interval - 1
            Intervals = Interval
            if Interval < 1:
               Interval = 1
               Intervals = Interval

         if z == 71 :
            rpibr = rpibr + 2
            if rpibr >= 100:
               rpibr = 100
            if camera_connected == 1 and use_Pi_Cam == 0:
               cam.set_controls(0,0,rpibr)
            restart = 1
         if z == 61 :
            rpibr = rpibr - 2
            if rpibr <= 0:
               rpibr = 0
            if camera_connected == 1 and use_Pi_Cam == 0:
               cam.set_controls(0,0,rpibr)
            restart = 1
         if z == 62 :
            rpico = rpico - 5
            if rpico <= -100:
               rpico = -100
            restart = 1
         if z == 72 :
            rpico = rpico + 5
            if rpico >= 100:
               rpico = 100
            restart = 1
         if z == 63:
            if rpiss <= 490000:
               rpiss = rpiss - 10000
            else:
               rpiss = rpiss - 100000
            if rpiss <= 10000:
               rpiss = 10000
            restart = 1   
         if z == 73:
            if rpiss <= 490000:
               rpiss = rpiss + 10000
            else:
               rpiss = rpiss + 100000
            if rpiss >= 6000000:
               rpiss = 6000000
            restart = 1   
         if z == 65 :
            if rpiISO > 0:
               rpiISO = rpiISO - 100
               rpiev = 0
            if rpiISO <= 0:
               rpiISO = 0
            restart = 1   
         if z == 75 :
            rpiISO = rpiISO + 100
            rpiev = 0
            if rpiISO >= 800:
               rpiISO = 800
            restart = 1   
         if z == 132:
            auto_c = auto_c + 1
            auto_g = 1
            auto_win = 1
            if auto_c > 1:
               auto_c = 0
               auto_win = 0
            keys2 ("cen",14,auto_c,b3x+37,b3y+65,0)
            keys2 ("tre",14,auto_c,b3x+40,b3y+77,0)

         if z == 105 and auto_g == 1:
            cls = cls + 1
            if cls > 1:
               cls = 0


         if z == 52:
            auto_win =  0
            crop  = crop  + 5
            if crop  > maxwin:
               crop  = maxwin
            if ((width/2) + offset3 + (crop /2) ) >= width:
               crop  = crop  - 5
            if ((width/2) + offset3 - (crop /2) ) <=1:
               crop  = crop  - 5
            if ((height/2) + offset4 + (crop /2) ) >= height:
               crop  = crop  - 5
            if ((height/2) + offset4 - (crop /2) ) <=1:
               crop  = crop  - 5

         if z == 42:
            auto_win = 0
            crop  = crop  - 5
            if crop  < minwin:
               crop  = minwin

         if (z == 101 or z == 111) and photoon == 1:
            photo = photo + 1
            achange = 1
            if photo > 1:
               photo = 0
               achange = 0
               camera = 0
               button2 (b2x+129,b2y+33,63,31,0,photo)#
               keys2 ("PHOTO",12,photo,b2x+137,b2y+35,0)#
               if use_RPiGPIO == 1:
                  GPIO.output(C_OP,GPIO.LOW)
            if photo == 1:
               pcount2 = pcount
               button2 (b2x+129,b2y+33,63,31,0,photo)#
               keys2 ("PHOTO",12,photo,b2x+137,b2y+35,0)#
               keys2 ("1",12,photo,b2x+153,b2y+48,0)
               ptime2 = time.time() + ptime
               camera = 1
               if use_RPiGPIO == 1:
                  GPIO.output(C_OP,GPIO.HIGH)
         if z == 112 and photoon == 1 and photo == 0:
            ptime = ptime + 10
            if ptime > 990:
               ptime = 990
         if z == 102 and photoon == 1 and photo == 0:
            ptime = ptime - 10
            if ptime < 10:
               ptime = 10
         if z == 113 and photoon == 1 and photo == 0:
            pcount = pcount + 1
            if pcount > 90:
               pcount = 90
         if z == 103 and photoon == 1 and photo == 0:
            pcount = pcount - 1
            if pcount < 0:
               pcount = 0
         if z == 91:
            nscale = nscale + 10
            if nscale > 800:
               nscale = 800
         if z == 81:
            nscale = nscale - 10
            if nscale < 10:
               nscale = 10
         if z == 93:
            escale = escale + 10
            if escale > 800:
               escale = 800
         if z == 83:
            escale = escale - 10
            if escale < 10:
               escale = 10
         if z == 92:
            sscale = sscale + 10
            if sscale > 800:
               sscale = 800
         if z == 82:
            sscale = sscale - 10
            if sscale < 10:
               sscale = 10
         if z == 94:
            wscale = wscale + 10
            if wscale > 800:
               wscale = 800
         if z == 84:
            wscale = wscale - 10
            if wscale < 10:
               wscale = 10
         if z == 95:
            nscale = nscale + 10
            if nscale > 800:
               nscale = 800
            wscale = wscale + 10
            if wscale > 800:
               wscale = 800
            sscale = sscale + 10
            if sscale > 800:
               sscale = 800
            escale = escale + 10
            if escale > 800:
               escale = 800
         if z == 85:
            wscale = wscale - 10
            if wscale < 10:
               wscale = 10
            escale = escale - 10
            if escale < 10:
               escale = 10
            sscale = sscale - 10
            if sscale < 10:
               sscale = 10
            nscale = nscale - 10
            if nscale < 10:
               nscale = 10
         if z == 51:
            frames = frames +1
            if frames > 4:
               frames = 1
         if z == 41:
            frames = frames -1
            if frames < 1:
               frames = 4
         if z == 43:
            Sens = Sens -1
            auto_t = 0
            if Sens < 1:
               Sens = 1
         if z == 53:
            Sens = Sens +1
            auto_t = 0
            if Sens > 100:
               Sens = 100
         if z == 133 or kz == K_DOWN:
            if serial_connected == 1:
               tim = mincor * 10
               blan = "0000" + str(int(tim))
               move = ':Mgs' + blan[(len(blan))-4:len(blan)]
               keys2 ("S",18,1,b3x+42,b3y + 102,1)
               lx200 (move,':Mgw0000')
               keys2 ("S",18,6,b3x+42,b3y + 102,1)
            if use_RPiGPIO == 1:
               keys2 ("S",18,1,b3x+42,b3y + 102,1)
               GPIO.output(S_OP,GPIO.HIGH)
               GPIO.output(N_OP,GPIO.LOW)
               keys2 ("N",18,6,b3x+42,b3y+38,1)
               time.sleep(mincor/100)
               keys2 ("S",18,6,b3x+42,b3y + 102,1)
               GPIO.output(S_OP,GPIO.LOW)

         if z == 131 or kz == K_UP:
            if serial_connected == 1:
               tim = mincor * 10
               blan = "0000" + str(int(tim))
               move = ':Mgn' + blan[(len(blan))-4:len(blan)]
               keys2 ("N",18,1,b3x+42,b3y + 38,1)
               lx200 (move,':Mgw0000')
               keys2 ("N",18,6,b3x+42,b3y + 38,1)
            if use_RPiGPIO == 1:
               keys2 ("N",18,1,b3x+42,b3y + 38,1)
               GPIO.output(N_OP,GPIO.HIGH)
               GPIO.output(S_OP,GPIO.LOW)
               keys2 ("S",18,6,b3x+42,b3y + 102,1)
               time.sleep(mincor/100)
               keys2 ("N",18,6,b3x+42,b3y+38,1)
               GPIO.output(N_OP,GPIO.LOW)

         if z == 122 or kz == K_LEFT:
            if serial_connected == 1:
               tim = mincor * 10
               blan = "0000" + str(int(tim))
               move = ':Mgw' + blan[(len(blan))-4:len(blan)]
               keys2 ("W",18,1,b3x+8,b3y + 70,1)
               lx200 (move,':Mgs0000')
               keys2 ("W",18,6,b3x+8,b3y + 70,1)
            if use_RPiGPIO == 1:
               keys2 ("W",18,1,b3x+8,b3y + 70,1)
               GPIO.output(W_OP,GPIO.HIGH)
               GPIO.output(E_OP,GPIO.LOW)
               keys2 ("E",18,6,b3x+75,b3y+70,1)
               time.sleep(mincor/100)
               keys2 ("W",18,6,b3x+8,b3y+70,1)
               GPIO.output(W_OP,GPIO.LOW)

         if z == 142 or kz == K_RIGHT:
            if serial_connected == 1:
               tim = mincor * 10
               blan = "0000" + str(int(tim))
               move = ':Mge' + blan[(len(blan))-4:len(blan)]
               keys2 ("E",18,1,b3x+75,b3y + 70,1)
               lx200 (move,':Mgs0000')
               keys2 ("E",18,6,b3x+75,b3y + 70,1)
            if use_RPiGPIO == 1:
               keys2 ("E",18,1,b3x+75,b3y + 70,1)
               GPIO.output(E_OP,GPIO.HIGH)
               GPIO.output(W_OP,GPIO.LOW)
               keys2 ("W",18,6,b3x+8,b3y+70,1)
               time.sleep(mincor/100)
               keys2 ("E",18,6,b3x+75,b3y+70,1)
               GPIO.output(E_OP,GPIO.LOW)

         if z == 143  or kz == K_END:
            auto_g = 0
            if serial_connected == 1:
               lx200 (':Q#',':Q#')
            if use_RPiGPIO == 1:
               GPIO.output(N_OP,GPIO.LOW)
               GPIO.output(S_OP,GPIO.LOW)
               GPIO.output(E_OP,GPIO.LOW)
               GPIO.output(W_OP,GPIO.LOW)
            keys2 ("N",18,6,b3x+42,b3y+38,1)
            keys2 ("S",18,6,b3x+42,b3y+102,1)
            keys2 ("W",18,6,b3x+8,b3y+70,1)
            keys2 ("E",18,6,b3x+75,b3y+70,1)


         if z == 24 or z == 34:
               auto_i = auto_i +1
               if auto_i > 1:
                  auto_i = 0

         if z == 1 or z == 11 or z == 21 or z == 31  or kz == K_a:
            auto_g = auto_g +1
            if auto_g == 1:
               start = time.time()
               achange = 1
               xcount = 0
               ycount = 0
               totvcor = 0
               tothcor = 0
               button2 (b2x+129,b2y+161,31,31,0,0)
               keys2 ("CLS",12,0,b2x+132,b2y+170,0)
               if aveon == 1:
                  button2 (353,161,31,31,0,0)
                  keys2 ("AVE",12,0,b2x+164,b2y+170,0)
               count = 1
               while count <= avemax:
                  xvcor[count] = 0
                  xhcor[count] = 0
                  count +=1
            if auto_g > 1:
               auto_g = 0
               achange = 0
               cls = 0
               ave = 0
               pygame.draw.rect(windowSurfaceObj,blackColor,Rect(b2x+127,b2y+160, 34, 32))

         if z == 5 or z == 15  or kz == K_t:
            thres = thres + 1
            achange = 1
            if thres > 1:
               thres = 0
               achange = 0
               change = 1
         if z == 4 or z == 14  or kz == K_g:
            graph = graph + 1
            if graph > 1:
               graph = 0
               if Display != 0:
                  pygame.draw.rect(windowSurfaceObj,blackColor,Rect(width + 64,19,53,259))
         if z == 2 or z == 12  or kz == K_p:
            plot = plot + 1
            if plot > 1:
               plot = 0
               if Display != 0:
                  pygame.draw.rect(windowSurfaceObj,blackColor,Rect(width + 9,19,53,259))

         if z == 22 or z == 32:
            auto_win = auto_win +1
            achange = 1
            if auto_win > 1:
               auto_win = 0
               achange = 0
               change = 1
         if z == 23 or z == 33:
            auto_t = auto_t +1
            achange = 1
            if auto_t > 1:
               auto_t = 0
               achange = 0
               change = 1
         if z == 74:
            if rpiexno < 8:
               rpiexno = rpiexno + 1
               rpiex = rpimodes[rpiexno]
            restart = 1   
         if z == 64:
            if rpiexno > 0:
               rpiexno = rpiexno - 1
               rpiex = rpimodes[rpiexno]
            restart = 1  
         if z == 3 or z == 13 or kz == K_l:
            log = log + 1
            if log > 1:
               log = 0
            if log == 1:
               now = datetime.datetime.now()
               timestamp = now.strftime("%y%m%d%H%M%S")   
               logfile = "/run/shm/" + str(timestamp) + ".txt"
               keys2 (" Logging to " + logfile,16,3,0,height-15,1)
               time.sleep(2)
               keys2 (" Logging to " + logfile,16,8,0,height-15,1)
         if z == 55 :
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
            if crop_img < max_res:
               crop_img = crop_img + 1
               if camera_connected == 1:
                  w = rpiwidth[crop_img]
                  h = rpiheight[crop_img]
                  scalex = rpiscalex[crop_img]
                  scaley = rpiscaley[crop_img]
                  nscale = nscale / scaley
                  escale = escale / scalex
                  sscale = sscale / scaley
                  wscale = wscale / scalex
                  offset5 = int((offset5 + offset3) * scalex)
                  offset6 = int((offset6 + offset4) * scaley)
                  if offset5 > 0 and offset5 >= (w/2)-(width/2):
                     offset5 = (w/2)-(width/2)
                  if offset5 < 0 and offset5 <= 0-((w/2)-(width/2)):
                     offset5 = 0-((w/2)-(width/2))
                  if offset6 > 0 and offset6 >= (h/2)-(height/2):
                     offset6 = (h/2)-(height/2)
                  if offset6 < 0 and offset6 <= 0-((h/2)-(height/2)):
                     offset6 = 0-((h/2)-(height/2))
                  offset3 = 0
                  offset4 = 0
                  if use_Pi_Cam == 0 and camera_connected == 1:
                     cam.stop()
                     pygame.camera.init()
                     if crop_img == 0:
                        cam = pygame.camera.Camera("/dev/video0",(320,240))
                     if crop_img == 1 and max_res >= 1:
                        cam = pygame.camera.Camera("/dev/video0",(352,288))
                     if crop_img == 2 and max_res >= 2:
                        cam = pygame.camera.Camera("/dev/video0",(640,480))
                     if crop_img == 3 and max_res >= 3:
                        cam = pygame.camera.Camera("/dev/video0",(800,600))
                     if crop_img == 4 and max_res >= 4:
                        cam = pygame.camera.Camera("/dev/video0",(960,720))
                     if crop_img == 5 and max_res >= 5:
                        cam = pygame.camera.Camera("/dev/video0",(1280,960))
                     if crop_img == 6 and max_res >= 6:
                        cam = pygame.camera.Camera("/dev/video0",(1920,1440))
                     if crop_img == 7 and max_res >= 7:
                        cam = pygame.camera.Camera("/dev/video0",(2592,1944))
                     cam.start()
                  if crop_img == 0:
                     offset3 = offset3/2
                     offset4 = offset4/2
               if camera_connected == 0:
                  wd = 20 + (crop_img * 4)
                  hd = wd
            restart = 1  
         if z == 45 :
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
            if crop_img > min_res:
               crop_img = crop_img - 1
               if camera_connected == 1:
                  w = rpiwidth[crop_img]
                  h = rpiheight[crop_img]
                  scalex = rpiscalex[crop_img +1]
                  scaley = rpiscalex[crop_img +1]
                  nscale = nscale * scaley
                  escale = escale * scalex
                  sscale = sscale * scaley
                  wscale = wscale * scalex
                  offset5 = int((offset5 + offset3) / scalex)
                  offset6 = int((offset6 + offset4) / scaley)
                  if offset5 > 0 and offset5 >= (w/2)-(width/2):
                     offset5 = (w/2)-(width/2)
                  if offset5 < 0 and offset5 <= 0-((w/2)-(width/2)):
                     offset5 = 0-((w/2)-(width/2))
                  if offset6 > 0 and offset6 >= (h/2)-(height/2):
                     offset6 = (h/2)-(height/2)
                  if offset6 < 0 and offset6 <= 0-((h/2)-(height/2)):
                     offset6 = 0-((h/2)-(height/2))
                  offset3 = 0
                  offset4 = 0
                  if use_Pi_Cam == 0:
                     cam.stop()
                     pygame.camera.init()
                     if crop_img == 0:
                        cam = pygame.camera.Camera("/dev/video0",(320,240))
                     if crop_img == 1 and max_res >= 1:
                        cam = pygame.camera.Camera("/dev/video0",(352,288))
                     if crop_img == 2 and max_res >= 2:
                        cam = pygame.camera.Camera("/dev/video0",(640,480))
                     if crop_img == 3 and max_res >= 3:
                        cam = pygame.camera.Camera("/dev/video0",(800,600))
                     if crop_img == 4 and max_res >= 4:
                        cam = pygame.camera.Camera("/dev/video0",(960,720))
                     if crop_img == 5 and max_res >= 5:
                        cam = pygame.camera.Camera("/dev/video0",(1280,960))
                     if crop_img == 6 and max_res >= 6:
                        cam = pygame.camera.Camera("/dev/video0",(1920,1440))
                     if crop_img == 7 and max_res >= 7:
                        cam = pygame.camera.Camera("/dev/video0",(2592,1944))
                     cam.start()
                  if crop_img == 0:
                     offset3 = offset3/2
                     offset4 = offset4/2
               if camera_connected == 0:                
                  wd = 20 + (crop_img * 4)
                  hd = wd
            restart = 1
         if z == 25:
            nsi = nsi + 1
            if nsi > 1:
               nsi = 0
         if z == 35:
            ewi = ewi + 1
            if ewi > 1:
               ewi = 0
         if (z == 155 or z == 165 or z == 175 or z == 154 or z == 164 or z == 174 or kz == K_4 or kz == K_5 or kz == K_6):
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
            if kz == K_4 or z == 154:
               z = 155
            if kz == K_5 or z == 164:
               z = 165
            if kz == K_6 or z == 174:
               z = 175
            deffile = "config" + str((z-145)/10)
            if z == 155:
               keys2 ("S1",14,1,b3x+8+96,b3y+150,0)
            if z == 165:
               keys2 ("S2",14,1,b3x+40+96,b3y+150,0)
            if z == 175:
               keys2 ("S3",14,1,b3x+72+96,b3y+150,0)
            pygame.display.update()
            fil = "0000"
            timp = str(auto_g) + fil[len(str(int(nscale))):len(str(int(nscale)))+4] + str(int(nscale)) + fil[len(str(int(sscale))):len(str(int(sscale)))+4] + str(int(sscale))
            timp = timp + fil[len(str(int(escale))):len(str(int(escale)))+4] + str(int(escale)) + fil[len(str(int(wscale))):len(str(int(wscale)))+4] +str(int(wscale)) + str(ewi)
            timp = timp + str(nsi) +fil[len(str(crop)):len(str(crop))+4] + str(crop)
            if offset3 < 0:
               offset3a = 0-int(offset3)
               timp = timp + "9"
            else:
               offset3a = int(offset3)
               timp = timp + "0"
            timp = timp + fil[len(str(offset3a))+1:len(str(offset3a))+4] + str(offset3a)
            if offset5 < 0:
               offset5a = 0-int(offset5)
               timp = timp + "9"
            else:
               offset5a = int(offset5)
               timp = timp + "0"
            timp = timp + fil[len(str(offset5a))+1:len(str(offset5a))+4] + str(offset5a)
            if offset6 < 0:
               offset6a = 0-int(offset6)
               timp = timp + "9"
            else:
               offset6a = int(offset6)
               timp = timp + "0"
            timp = timp + fil[len(str(offset6a))+1:len(str(offset6a))+4] + str(offset6a)
            if offset4 < 0:
               offset4a = 0-int(offset4)
               timp = timp + "9"
            else:
               offset4a = int(offset4)
               timp = timp + "0"
            timp = timp + fil[len(str(offset4a))+1:len(str(offset4a))+4] + str(offset4a) + fil[len(str(Intervals)):len(str(Intervals))+4] + str(Intervals) + str(log)
            timp = timp + str(frames) + fil[len(str(Sens)):len(str(Sens))+4] + str(Sens) + str(thres) + str(graph) + str(auto_i) + str(plot)
            timp = timp + str(auto_win) + str(auto_t) + str(crop_img)
            timp = timp + fil[len(str(rpibr)):len(str(rpibr))+4] + str(rpibr)
            if rpico < 0:
               rpicoa = 0-rpico
               timp = timp + "9"
            else:
               rpicoa = rpico
               timp = timp + "0"
            timp = timp + fil[len(str(rpicoa))+1:len(str(rpicoa))+4] + str(rpicoa)                         
            if rpiev < 0:
               rpieva = 0-rpiev
               timp = timp + "9"
            else:
               rpieva = rpiev
               timp = timp + "0"
            timp = timp + fil[len(str(rpieva))+1:len(str(rpieva))+4] + str(rpieva) + fil[len(str(int(rpiss/1000))):len(str(int(rpiss/1000)))+4] + str(int(rpiss/1000)) + fil[len(str(rpiISO)):len(str(rpiISO))+4] + str(rpiISO) +str(rpiexno)

            file = open(deffile + ".txt", "w")
            file.write(timp)
            file.close()
            #time.sleep(1)
            if z == 155:
               keys2 ("S1",14,6,b3x+8+96,b3y+150,0)
            if z == 165:
               keys2 ("S2",14,6,b3x+40+96,b3y+150,0)
            if z == 175:
               keys2 ("S3",14,6,b3x+72+96,b3y+150,0)

            restart = 1
         if (z == 125 or z == 135 or z == 145 or z == 124 or z == 134 or z == 144 or kz == K_1 or kz == K_2 or kz == K_3):
            if use_Pi_Cam == 1 and camera_connected == 1:
               os.killpg(p.pid, signal.SIGTERM)
            if kz == K_1 or z == 124:
               z = 125
            if kz == K_2 or z == 134:
               z = 135
            if kz == K_3 or z == 144:
               z = 145
            deffile = "config" + str((z-115)/10)
            if z == 125:
               keys2 ("R1",14,1,b3x+8,b3y+150,0)
            if z == 135:
               keys2 ("R2",14,1,b3x+40,b3y+150,0)
            if z == 145:
               keys2 ("R3",14,1,b3x+72,b3y+150,0)
            pygame.display.update()
            if os.path.exists(deffile + ".txt") == True:
               file = open(deffile + ".txt","r")
               inputx = file.readline()
               file.close()
               auto_g = int(inputx[0:1])
               nscale = int(inputx[1:5])
               sscale = int(inputx[5:9])
               escale = int(inputx[9:13])
               wscale = int(inputx[13:17])
               ewi = int(inputx[17:18])
               nsi = int(inputx[18:19])
               crop = int(inputx[19:23])
               offset3 = int(inputx[23:27])
               if offset3 > 9000:
                  offset3 = 0-(offset3-9000)
               offset5 = int(inputx[27:31])
               if offset5 > 9000:
                  offset5 = 0-(offset5-9000)
               offset6 = int(inputx[31:35])
               if offset6 > 9000:
                  offset6 = 0-(offset6-9000)
               offset4 = int(inputx[35:39])
               if offset4 > 9000:
                  offset4 = 0-(offset4-9000)
               Intervals = int(inputx[39:43])
               log2 = log
               log = int(inputx[43:44])
               if log == 1 and log2 == 0:
                  now = datetime.datetime.now()
                  timestamp = now.strftime("%y%m%d%H%M%S")
                  logfile = "/tmp/" + str(timestamp) + ".txt"
                  keys2 (" Logging to " + logfile,16,3,0,height-15,1)
                  time.sleep(2)
                  keys2 (" Logging to " + logfile,16,8,0,height-15,1)
               frames = int(inputx[44:45])
               Sens = int(inputx[45:49])
               thres = int(inputx[49:50])
               graph = int(inputx[50:51])
               auto_i = int(inputx[51:52])
               plot = int(inputx[52:53])
               auto_win = int(inputx[53:54])
               auto_t = int(inputx[54:55])
               if camera_connected == 1 :
                  crop_img = int(inputx[55:56])
               if Image_window > crop_img:
                  crop_img = Image_window
               if crop_img == 0:
                  w = width
                  h = height
               if crop_img > 0:
                  w = rpiwidth[crop_img]
                  h = rpiheight[crop_img]

               if use_Pi_Cam == 0:
                  cam.stop()
                  pygame.camera.init()
                  if crop_img == 0:
                     cam = pygame.camera.Camera("/dev/video0",(320,240))
                  if crop_img == 1 and max_res >= 1:
                     cam = pygame.camera.Camera("/dev/video0",(352,288))
                  if crop_img == 2 and max_res >= 2:
                     cam = pygame.camera.Camera("/dev/video0",(640,480))
                  if crop_img == 3 and max_res >= 3:
                     cam = pygame.camera.Camera("/dev/video0",(800,600))
                  if crop_img == 4 and max_res >= 4:
                     cam = pygame.camera.Camera("/dev/video0",(960,720))
                  if crop_img == 5 and max_res >= 5:
                     cam = pygame.camera.Camera("/dev/video0",(1280,960))
                  if crop_img == 6 and max_res >= 6:
                     cam = pygame.camera.Camera("/dev/video0",(1920,1440))
                  if crop_img == 7 and max_res >= 7:
                     cam = pygame.camera.Camera("/dev/video0",(2592,1944))
                  cam.start()
               if camera_connected == 0:
                  crop_img = int(inputx[55:56])
                  wd = 20 + (crop_img * 4)
                  hd = wd
               rpibr = int(inputx[56:60])
               rpico = int(inputx[60:64])
               if rpico > 9000:
                  rpico = 0-(rpico-9000)
               rpiev = int(inputx[64:68])
               if rpiev > 9000:
                  rpiev = 0-(rpiev-9000)
               rpiss = (int(inputx[68:72]))*1000
               rpiISO = int(inputx[72:76])
               rpiexno = int(inputx[76:77])
               rpiex = rpimodes[rpiexno]
            if z == 125:
               keys2 ("R1",14,6,b3x+8,b3y+150,0)
            if z == 135:
               keys2 ("R2",14,6,b3x+40,b3y+150,0)
            if z == 145:
               keys2 ("R3",14,6,b3x+72,b3y+150,0)

            restart = 1

         if use_Pi_Cam == 1 and restart == 1 and camera_connected == 1:
            if os.path.exists('/run/shm/test.jpg') == True:
               os.rename('/run/shm/test.jpg','/run/shm/oldtest.jpg')
            try:
               os.remove('/run/shm/test.jpg')
            except OSError:
               pass

            rpistr = "raspistill -o /run/shm/test.jpg -co " + str(rpico) + " -br " + str(rpibr)
            if rpiex != 'off':
               rpistr = rpistr + " -t " + str(rpit) + " -tl 0 -ex " + rpiex
            else:
               rpistr = rpistr + " -t " + str(rpit) + " -tl 0 -ss " + str(rpiss) + " -fp -bm -awb off -awbg 1,1.3"
            if rpiISO > 0:
               rpistr = rpistr + " -ISO " + str(rpiISO)
            if rpiev != 0:
               rpistr = rpistr + " -ev " + str(rpiev)
            rpistr = rpistr + " -n -sa " + str(rpisa)
            off5 = (Decimal(0.5) - (Decimal(width)/Decimal(2))/Decimal(w)) + (Decimal(offset5)/Decimal(w))
            off6 = (Decimal(0.5) - (Decimal(height)/Decimal(2))/Decimal(h)) + (Decimal(offset6)/Decimal(h))
            widx = Decimal(width)/Decimal(w)
            heiy = Decimal(height)/Decimal(h)
            rpistr = rpistr + " -q 100 -w " + str(width) + " -h " + str(height) + " -roi " +  str(off5) + "," + str(off6) + ","+str(widx) + "," + str(heiy)
            #print rpistr
            p=subprocess.Popen(rpistr,shell=True, preexec_fn=os.setsid)    

 

Vu qu'en ce moment je suis périodiquement en déplacements, je compte me faire un petit montage avec un potard numérique & un arduino le tout relié sur un pc pour faire de l'autoguidage sur une eq2.

Rien à voir avec le raspberry, mais ça pourrait être une solution lowcost intéressante ;)

Lien vers le commentaire
Partager sur d’autres sites

Je vais me pencher sur le suivi ce weekend je pense.

Je me suis fait le weekend dernier un intervallomètre avec un arduino (port com & gestion des prises de vue avec un autre soft que j'ai fait sous delphi).

Je pense l'associer avec le suivi et faire la gestion des dark dans la foulée.

Ca occupe vu qu'en ce moment, tous les weekend sont pourris...

Dés que je pourrai tester tout ça, je posterai la bricole en ligne.

Lien vers le commentaire
Partager sur d’autres sites

Rejoignez la conversation !

Vous pouvez répondre maintenant et vous inscrire plus tard. Si vous avez un compte, connectez-vous pour poster avec votre compte.

Invité
Répondre à ce sujet…

×   Collé en tant que texte enrichi.   Coller en tant que texte brut à la place

  Seulement 75 émoticônes maximum sont autorisées.

×   Votre lien a été automatiquement intégré.   Afficher plutôt comme un lien

×   Votre contenu précédent a été rétabli.   Vider l’éditeur

×   Vous ne pouvez pas directement coller des images. Envoyez-les depuis votre ordinateur ou insérez-les depuis une URL.

  • En ligne récemment   0 membre est en ligne

    • Aucun utilisateur enregistré regarde cette page.
×
×
  • Créer...

Information importante

Nous avons placé des cookies sur votre appareil pour aider à améliorer ce site. Vous pouvez choisir d’ajuster vos paramètres de cookie, sinon nous supposerons que vous êtes d’accord pour continuer.