botSimMain.py 2.52 KB
Newer Older
1
2
3
import logging.config
import logging
import vrep
4
5
6
import time
import numpy as np
import sys
7
import subprocess
8
9
10
11
12

from ePucks import EPuckSim
from myThreads import ControllerThread

logger = logging.getLogger(__name__)
13
14
logger_b = logging.getLogger('beliefLogger')
logger_p = logging.getLogger('poolLogger')
15
16


Chanelle Lee's avatar
Chanelle Lee committed
17
def main(numSims=1):
18
    logging.config.fileConfig('configuration.ini')
Chanelle Lee's avatar
Chanelle Lee committed
19
    # Connect to V-REP
20
    subprocess.call("")
21
    clientID = vrep.simxStart('127.0.0.1', 19997, True, True, 5000, 5)
22
    if clientID != -1:
23
        logger.info('Connected to remote API server')
24
    else:
25
26
27
28
29
30
31
32
33
34
        logger.critical('Could not connect to streaming server')
    time.sleep(5)
    for i in range(0, numSims):
        sretCode = vrep.simxStartSimulation(clientID, vrep.simx_opmode_oneshot)
        if sretCode > 1:
            logger.critical('Simulation not started! {}'.format(sretCode))
            sys.exit('Could not start simulation {}'.format(sretCode))
        time.sleep(5)
        logger_b.info('Now on simulation {}'.format(i))
        logger_p.info('Now on simulation {}'.format(i))
35
36
37
38
39
40
41
42
43
44
45
46
47
48
        try:
            controllerJobs = setUp()
            # Start all threads
            for p in controllerJobs:
                p.start()
                time.sleep(0.1)
            # Wait for all threads to finish
            for p in controllerJobs:
                p.wait()
        except KeyboardInterrupt:
            print "Ctrl-C pressed..."
            for p in controllerJobs:
                p.stop()
            sys.exit(1)
49
50
51
        time.sleep(5)
        eretCode = vrep.simxStopSimulation(clientID, vrep.simx_opmode_oneshot)
        if eretCode > 1:
Chanelle Lee's avatar
Chanelle Lee committed
52
53
            logger.critical('Simulation not finished'
                            + ' properly! {}'.format(eretCode))
54
            sys.exit('Could not finish simulation {}'.format(eretCode))
Chanelle Lee's avatar
Chanelle Lee committed
55
        time.sleep(240)
56
57


58
def setUp(noise):
59
    optionsList = [0, 1, 2, 3, 4]
60
    w = 1.0
61
    noise = noise
62
    poolSize = 2
63
    numEpucks = 5  # 20
64
    ePucks = []
Chanelle Lee's avatar
Chanelle Lee committed
65
66
    populationBeliefs = (1.0/len(optionsList)) * np.ones((numEpucks,
                                                          len(optionsList)))
67
68

    for i, p in enumerate(populationBeliefs):
69
        ePucks.append(EPuckSim(i, p, w, optionsList, noise, poolSize))
70
71
72
73
        time.sleep(0.1)

    controllerJobs = [ControllerThread(name='controller{}'.format(e.ID),
                                       ePuck=e) for e in ePucks]
74
    return controllerJobs
75

76
77
78

if __name__ == '__main__':
    main()