Example #1
0
 def test_program_with_variable(self):
     commands = """
     main() {
         int distance <- 3;
         int angle <- 90;
         takeoff(); 
         forward(distance); 
         rotate_left(angle); 
         forward(distance); 
         rotate_left(angle);
         land();
     }
     """
     target = [{
         "action": "takeoff",
         "value": []
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "land",
         "value": []
     }]
     self.assertEqual(target, generate_simulation_json(commands))
Example #2
0
 def test_square_with_turning(self):
     commands = "main() {takeoff(); forward(3); rotate_left(90); forward(3); rotate_left(90); forward(3); rotate_left(90); forward(3); rotate_left(90); land();}"
     target = [{
         "action": "takeoff",
         "value": []
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "land",
         "value": []
     }]
     self.assertEqual(target, generate_simulation_json(commands))
Example #3
0
def main():
    logging.basicConfig(level=logging.INFO)
    while True:
        code = input('> ')
        try:
            print(generate_simulation_json(code))
        except Exception as e:
            logging.error(e)
Example #4
0
 def test_basic_program(self):
     commands = "main() {takeoff(); land();}"
     target = [{
         "action": "takeoff",
         "value": []
     }, {
         "action": "land",
         "value": []
     }]
     self.assertEqual(target, generate_simulation_json(commands))
Example #5
0
 def test_square_using_two_repeats(self):
     commands = """
       main() {
         takeoff();
         repeat 2 times {
           repeat 2 times {
             forward(3); rotate_left(90);
           }
         }
         land();
       }"""
     target = [{
         "action": "takeoff",
         "value": []
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "land",
         "value": []
     }]
     self.assertEqual(target, generate_simulation_json(commands))
Example #6
0
 def test_program_with_list_variable(self):
     commands = """
     main() {
         list[int] distances <- [3, 4];
         list[int] angles <- [90, 180];
         takeoff(); 
         forward(distances[0]); 
         rotate_left(angles[0]); 
         forward(distances[1]); 
         rotate_left(angles[1]);
         distances[1] <- 3;
         angles[1] <- 90;
         forward(distances[1]); 
         rotate_left(angles[1]);
         land();
     }
     """
     target = [{
         "action": "takeoff",
         "value": []
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "forward",
         "value": [4]
     }, {
         "action": "rotateL",
         "value": [pi]
     }, {
         "action": "forward",
         "value": [3]
     }, {
         "action": "rotateL",
         "value": [pi / 2]
     }, {
         "action": "land",
         "value": []
     }]
     self.assertEqual(target, generate_simulation_json(commands))
Example #7
0
 def test_square_no_turn(self):
     commands = "main() {takeoff(); forward(2); left(2); backward(2); right(2); land();}"
     target = [{
         "action": "takeoff",
         "value": []
     }, {
         "action": "forward",
         "value": [2]
     }, {
         "action": "left",
         "value": [2]
     }, {
         "action": "backward",
         "value": [2]
     }, {
         "action": "right",
         "value": [2]
     }, {
         "action": "land",
         "value": []
     }]
     self.assertEqual(target, generate_simulation_json(commands))
Example #8
0
def simulate():
    data = request.get_json()
    program = data['body']['body']['program']
    commands = xdrone.generate_simulation_json(program)
    return jsonify({'commands': commands})