1
00:00:00,420 --> 00:00:06,000
In this lesson we will be connecting these commands so the drone will move accordingly.

2
00:00:06,000 --> 00:00:11,880
But before we start setting the commands I want to make sure that the drone model won't be invoked multiple

3
00:00:11,880 --> 00:00:14,730
times under the models folder.

4
00:00:14,780 --> 00:00:19,790
We set the drone manager JPY once the drone manager is invoked.

5
00:00:19,800 --> 00:00:25,190
We want all the request to go to the same drone manager object to said this.

6
00:00:25,200 --> 00:00:29,610
We will be using a pattern called Singleton for many of you.

7
00:00:29,610 --> 00:00:32,760
You may not know what Singleton is.

8
00:00:32,760 --> 00:00:40,250
So let's go to Tools and write down what Singleton is right click on tools and choose new.

9
00:00:40,320 --> 00:00:46,040
Click on python file and set the file name to single in this file.

10
00:00:46,050 --> 00:00:50,080
We will be writing some sample codes to create Singleton.

11
00:00:50,400 --> 00:00:54,640
Singleton is a design pattern so you don't have to memorize the entire code.

12
00:00:55,380 --> 00:00:58,860
It's best that you understand how it is written set.

13
00:00:58,890 --> 00:01:00,540
Class singleton.

14
00:01:00,780 --> 00:01:06,510
Usually we would set object between the parentheses but in this case we will be using type.

15
00:01:07,410 --> 00:01:10,680
Let's take a look at what type can do the type.

16
00:01:10,680 --> 00:01:13,790
Class inherits the object in this class.

17
00:01:13,790 --> 00:01:18,250
There is a special method called call by setting type.

18
00:01:18,270 --> 00:01:23,160
We can use this called method to handle the class when it's invoked.

19
00:01:23,170 --> 00:01:25,330
Now let's go back to the code.

20
00:01:25,420 --> 00:01:29,350
Next we set instance which is a class attribute.

21
00:01:29,350 --> 00:01:32,400
Then we use the call method that we just checked earlier.

22
00:01:33,250 --> 00:01:44,230
Change the self to a class start an if statement if the CFS as in class is not in the CLEC instance.

23
00:01:44,340 --> 00:01:47,860
In the beginning there will not be a seamless set.

24
00:01:48,180 --> 00:01:49,110
Let's set to print.

25
00:01:49,110 --> 00:01:53,250
Call so you will see that CLSA is not set in the beginning.

26
00:01:53,250 --> 00:02:02,190
Next we set CFS instance with the CNS as a key and super set to Singleton and CFS followed by the call

27
00:02:02,190 --> 00:02:03,410
method.

28
00:02:03,420 --> 00:02:05,650
Here we set the arcs and K works

29
00:02:08,370 --> 00:02:13,570
the class instance will be set to this instance when the method is invoked.

30
00:02:13,590 --> 00:02:19,310
This class object will be created CFS will be set to the curly braces.

31
00:02:19,470 --> 00:02:22,110
The second time the method is invoked.

32
00:02:22,110 --> 00:02:27,750
The class already exists so that if statement will be a false and will skip printing call.

33
00:02:28,590 --> 00:02:32,160
Lastly we return the CLSA instance.

34
00:02:32,160 --> 00:02:37,170
Even if you didn't understand these codes once we run this code you will get a better understanding

35
00:02:37,980 --> 00:02:39,630
to redefine the class.

36
00:02:39,630 --> 00:02:48,990
We can do so by setting matter class to Singleton defined the init method let's set to print in it by

37
00:02:48,990 --> 00:02:49,870
setting this.

38
00:02:49,950 --> 00:02:52,620
The class t became a singleton.

39
00:02:52,770 --> 00:02:55,510
I'll show an example of what we just created.

40
00:02:55,620 --> 00:03:00,770
Set test to t followed by a parentheses we want to run this code.

41
00:03:00,780 --> 00:03:04,500
So let's go on to the edit configuration button.

42
00:03:04,500 --> 00:03:08,700
Currently the go button is set to the main P Y script.

43
00:03:08,700 --> 00:03:10,500
Let's add a new path.

44
00:03:10,530 --> 00:03:13,800
Click on the plus button and choose python.

45
00:03:13,890 --> 00:03:18,560
I'll set the name to single and set the script path to the one we just made.

46
00:03:18,890 --> 00:03:23,890
Good to be white Tello tools and choose single P Y and click open.

47
00:03:24,000 --> 00:03:31,170
Here you will see that run with python console is set on l object this button so you can see the screen

48
00:03:31,170 --> 00:03:33,480
better if you wish to leave it on.

49
00:03:33,480 --> 00:03:35,230
You could do so as well.

50
00:03:35,340 --> 00:03:42,630
Now hit Okay and let's run this code as we run this code you will see that the text call and in it has

51
00:03:42,630 --> 00:03:44,310
been printed.

52
00:03:44,310 --> 00:03:50,250
If we just do the process once it's hard to understand what is going on so I'll copy and paste the code

53
00:03:50,250 --> 00:03:54,190
for one two three four and five times.

54
00:03:54,240 --> 00:03:56,460
Now let's run the code again.

55
00:03:56,460 --> 00:04:00,360
Again we see the text call and in it has been printed.

56
00:04:00,360 --> 00:04:03,750
Next I will change what we said in the parenthesis from metal.

57
00:04:03,750 --> 00:04:09,420
Class equals Singleton to a regular object as we run this new code.

58
00:04:09,420 --> 00:04:13,100
You will see 5 in its printed.

59
00:04:13,120 --> 00:04:21,040
This means that each test is a different class object but if we use Singleton this text will be call

60
00:04:21,180 --> 00:04:29,700
in a single init printed this means once the class object T is created every time the object is invoked

61
00:04:30,060 --> 00:04:37,440
the process will refer to the same object T created in the beginning so that Singleton.

62
00:04:37,440 --> 00:04:40,220
Now let's quickly go through the script again.

63
00:04:40,500 --> 00:04:47,730
The t gets called and it is set to Singleton type when the call method is invoked and if there is nothing

64
00:04:47,730 --> 00:04:54,510
set in the instance the process will go on to super and create the class which will be set in the instances

65
00:04:56,280 --> 00:04:59,970
for the second time in after the column method is invoked.

66
00:04:59,970 --> 00:05:07,080
CLSA is set in the instances so the first CLSA will be returned no matter how many times the call method

67
00:05:07,080 --> 00:05:12,920
is is invoked the process will return the CFS which was set for the first time.

68
00:05:12,920 --> 00:05:16,190
I hope you understood how the Singleton works it.

69
00:05:16,340 --> 00:05:20,020
Even if you didn't understand each code you don't have to worry.

70
00:05:20,030 --> 00:05:24,860
This is called a design pattern which is also used in other programming languages.

71
00:05:24,860 --> 00:05:30,900
You can always keep this code as a sample and copy paste it when you need it.

72
00:05:30,920 --> 00:05:37,490
Now let's actually copy this code so we can set it in our project go to models folder and right click

73
00:05:38,210 --> 00:05:47,640
choose new and hit python file name the file base and head OK paste the Singleton code you just copied

74
00:05:47,640 --> 00:05:51,890
earlier.

75
00:05:51,930 --> 00:05:54,330
Now go to edit configuration.

76
00:05:54,720 --> 00:06:01,980
We won't be able to use a single button anymore suppress the minus button to delete single hit OK.

77
00:06:02,070 --> 00:06:02,970
And we are done.
