Implement Build-in Command in C

Which are the build-in commands?

A shell built-in command is contained in the shell itself (implemented by shell author) and runs on the same shell without creating a new process, e.g. cd, pwd, exit, alias.

By contrast: For a regular(linux) command, it is run from a binary file (found in $PATH or specified file) by forking the existing process in a subshell, e.g. ls, cat, rm.

How to implement

1. Judge whether is build-in command

Assume we have already got the parsed arguments

char * parsedArgs[MAXPARSE].

For example, a input char * input = "cd .." can be parsed as char * parsedArgs[MAXPARSE] = {"cd", ".."}

Then, we just need to judge whether parsedArgs[0] is contained in our build-in command set {"exit", "cd", "pwd" ...} (by using strcmp)

2. Implementation

In this project (as of milestone 2) , we need to implement exit, pwd, cd.

exit

In Bash, exit is a built-in command used to terminate the current shell session (same in our mumsh).

Each running shell is a process, so we can use the exit() in stdlib.h to terminate the shell process, achieving the goal of exiting the shell.

1
2
3
4
void exec_exit(){
printf("exit\n");
exit(0); // 0 for exit normally, otherwise abnormally
}

pwd

Running a crude (or minimal?) shell and have no idea where you are? Just type pwd and it will print the name of the current/working directory.

In C, we can implement the similar functionality using the getcwd() in unistd.h.

if success, getcwd() will return the pointer to the result, else ( e.g. no enough space) return NULL

1
2
3
4
void exec_pwd(){
char buf[1024]; // memory space need for storage
printf("%s\n", getcwd(buf,sizeof(buf)));
}

However, the working directory is not guaranteed to be shorter than 1024, so we may need to dynamically allocate the buffer to suit the length.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int size = 100;
char *buffer = malloc(size);

while (getcwd(buffer, size) == NULL) {
if (errno == ERANGE) { // buffer too small
size *= 2;
buffer = realloc(buffer, size);
} else { // other error
...
free(buffer);
return 1;
}
}

printf("%s\n", buffer);

free(buffer);

cd

cd is used for changing working directory.

In C, we can implement the similar functionality using the chdir() in unistd.h.

if success, chdir() will return 0, else ( e.g. no such directory) return -1

1
2
3
void exec_cd(char * path){
chdir(path);
}

Very straight forward, but if we want to implement more advanced features of the cd command such as changing to the home directory or the previous directory, we would need to handle these cases manually.

For example, you could check if the argument is ~ or -, and then call chdir() with the appropriate path

Aircraft control policy

Aircraft control policy

Aircraft control policy

State Variable

Intuitive

  • Aircraft
    • Rotation (in euler angle: x,y,z)
    • Position (In the world coordinate system:x,y,z)
    • Linear velocity (In the world coordinate system: x,y,z)
    • Angular velocity (In the world coordinate system: x,y,z)
    • Acceleration (In the world coordinate system:x,y,z)
  • Carrier Vessel Landing Runway
    • Position
    • Rotation (fixed)
    • Velocity (fixed)

Extensive

  • Aircraft
    • AoA (angle of attack 攻角)

Decision Variable

  • Aircraft
    • Pitch angle (control aircraft’s rotation along x axis)
    • Roll angle (control aircraft’s rotation along z axis)
    • Yaw angle (control aircraft’s rotation along y axis)
    • Throttle (control thrust)

Level 1 (Basic) Policy

Only contain the control over control surface (on control over throttle)

Attitude Control/Maintainance

Pitch(俯仰角) C/M

Function: KeepPitch()

Control the angle of horizontal tail (elevator)

  1. First keep the angle of elevator same with AoA(攻角), to avoid the disturbance of the horizontal tail on the attitide of the aircraft.

    1
    aircraft.PitchAngle = aircraft.AoA;

    这里的aircraft.PitchAngle指的是飞机升降舵的角度(下片为正)

  2. 根据当前俯仰角,俯仰速度,和目标俯仰角,调整升降舵面的角度

    1
    aircraft.PitchAngle += 5 * (p - aircraft.Pitch) / Mathf.Exp(10 * aircraft.PitchSpeed * Mathf.Sign(p - aircraft.Pitch));

Other Attitude C/M

与俯仰角类似(分别控制滚转舵面以及偏航舵面),不过不需要第一步

Level 2 Policy

Velocity Direction Control/Maintainance

Gliding Angle (下滑角)

Control the thrust of aircraft to change the angle of gliding

  1. 保持飞机的俯仰角不变(5度仰角)

    1
    KeepPitch(5f);
  2. 计算当前下滑角

    1
    float velAngle = 90 - Vector3.Angle(aircraft.rb.velocity, Vector3.up);
  3. 根据下滑角更改油门大小

    1
    aircraft.Thrust += (p - velAngle)/500f;

Level 3 Policy

Follow Trajectory

Vertical Approaching (竖直平面上接近预定下滑道)

通过调整飞机的下滑角来使飞机在竖直面上接近并维持在理想的下滑道

  1. 计算偏差对应的向量
    1
    Vector3 approachDirection = Vector3.ProjectOnPlane(route.Destination - transform.position, route.direction);
  2. 根据偏差值调整下滑角的大小(-3为目标下滑轨道的下滑角)
    1
    ThrustKeepGliding(-3 + Mathf.Clamp(approachDirection.y / 10f, -5,5));

Horizontal Approaching (水平面上接近预定下滑道)

通过调整飞机的偏航舵面角度(进而影响飞机的y轴rotation)来使飞机在水平面上接近并维持在理想的航道上

  1. 计算偏差对应的向量
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Vector3 approachDirection = Vector3.ProjectOnPlane(route.Destination - transform.position, route.direction);
    float distance = 0;

    if (Vector3.Angle(approachDirection, transform.right) < 90)
    {
    distance = Vector3.ProjectOnPlane(approachDirection, Vector3.up).magnitude;
    }
    else
    {
    distance = -Vector3.ProjectOnPlane(approachDirection, Vector3.up).magnitude;
    }
  2. 根据偏差值调整飞机的目标偏航角度(-8为目标下滑轨道的偏航角度)
    1
    KeepYaw(-8 + Mathf.Clamp(distance / 10f, -50, 50));

Get the ideal trajectory(补充)

基本的策略以及阐述完毕,那么所谓的理想下滑道到底应该怎么得到呢?
理想下滑道是一个射线,包含一个终点(飞机着陆的终点)和一个方向(飞机接近的方向)

1
2
3
4
5
public class Target_Route
{
public Vector3 Destination;
public Vector3 direction;
}

理想航道的偏航角和下滑角已知(分别为-8(平行于着陆跑道)和-3)
通过迭代飞机着陆所需时间来求得下滑道终点的位置

1
2
3
4
5
6
7
8
9
10
11
12
route.Destination = CV.transform.position;
route.direction = new Vector3(-0.14f, -0.05f, 0.99f).normalized;
// 获取飞机速度在下滑道方向上投影的大小
float axialVel = Vector3.Dot(aircraft.rb.velocity, route.direction);

// calculate precise destination
float FlightTime = 0;
for (int i = 0; i < 10; i++)
{
FlightTime = Vector3.Dot(route.Destination - aircraft.transform.position,route.direction) / axialVel;// 获取预计到达终点需要的时间
route.Destination = CV.transform.position + 15 * FlightTime * Vector3.forward; // 其中15是航母的速度,Vector3.forward是航母前进方向
}

autonomousDriving

Autonomous Driving

Perception

  • Navigation tool
  • Camera
  • Radar
  • LiDAR
  • Infrared detector
  • Speedometer
  • veh-veh communication
  • veh-infrastructure communication

Decision-making

  • departure time
  • good route
  • good lane
  • driving style
  • way to accel/decel
  • make a turn
  • park location

Architecture

  • Pre-trip

    trip generation, modal choice, routine choice

  • In-trip

    Path planning, Maneuver regulation

  • Post-trip

    Parking

Pre-trip decisions

  • Centralized

    central command center send instructions
    Some win, some lose

  • Decentralized

    plan trip independently, selfish
    little communication
    Sometime efficient, sometime inefficient

In-trip decisions

  • offline info

    static, in form of map

  • online info

    enable the route to be adapted to changing traffic conditions

Post-trip decision

Speed tracking

Dynamic system

  • State Variable

    $v[t]$

  • State space

    $R>= 0$

  • Control input

    $u[t]$(accel)

  • System dynamic

    $v[t+dt] = v[t]+u[t]*dt$

Open-loop control

Specify the control input $u[t]$ for all t in $[0,T]$
not a good idea for autonomous driving.

In practice, we need to consider the uncertainty of the system.
We should have:
$$
v[t+dt]=v[t]+u[t]dt+w(t)
$$
Where w(t) is the noise term capturing the uncertainty of the system.

Closed-loop control

Specify the control input $u[t]$ as a function of the state $v[t]$
able to adapt to the uncertainty of the system.
Compare the state $v[t]$ with the desired state $v_desired[t]$
Essentially, we need a mapping from state to control input
like map speed to acceleration
$$
v[t+dt]=v[t]+mu[v[t]]dt+w(t)
$$

Linear controller

the $u[t]$ has linear relationship with $v[t]$
$$
\mu[v] = k[v-v_{desired}]
$$

LTI system

linear time-invariant

Control of LTI system

exponential convergence is stronger than asymptotic convergent
if LTI system is asymptotic convergent, it is also exponential convergent
$$
x[t+1]=ax[t]+bu[t]
$$

Speed tracking

  • given:

    $v_{desired}, \delta$

  • determine:

    $u[t]$ $t = 0,1,2$

  • state

    $v[t]$
    $v[t+1] = v[t] + u[t]\delta + w[t]$

select $u[t]$ so that $w[t]$ will not accumulate

$u[t]=\mu(v[t]) = kv[t]$

$$
v[t+1] = f(v[t],u[t])
$$

Remember the review question

Trajectory tracking

Overview:

  • track means asymptotic convergent

  • $x[t]$ and $x_{desired}[t]$

  • $\lim_{t\rightarrow\infty}|x[t]-x_{desired}[t] | = 0$

  • state of system:$[x[t], v[t]]^T$

  • if successful:
    $$
    [x[t], v[t]]^T \rightarrow [x_{desired}[t], v_{desired}[t]]^T
    $$

Control policy

  • A control policy is a function
  • This function maps a state to a control input
    $$
    u[t] = f(x[t],v[t])
    $$

$$
u[t] = -k_1(x[t]-x_{desired}[t])-k_2(v[t]-v_{desired}[t])
$$

$$
k_1,k_2>0
$$

2023 Christmas

可供参考的资料

聊天记录:

  • 《照明商店》
  • 避开巨多人
  • WOKKA 舒芙蕾店,点评
  • 冰激凌店,
  • 武康路

武康路

![[Pasted image 20231223182758.png]]

2024 心怡姐姐22岁生日!出来玩大作战!

因为xyjj2/9的生日和除夕夜冲突啦,但是,想陪xyjj过生日,所以准备8号和xyjj出来玩。

备注:

  • 和xyjj一起买给妹妹买新年礼物(小玩意就好,作为妹妹送xyjj的线条小狗夹子的回礼)
  • 吃冰激凌店!
  • 记得定好晚餐
  • 一些其他活动
  • 啊,霓裳茶舞也有大悦城店

买礼物篇

吐槽. 正常情侣出来玩都是给对方买礼物,怎么到这里每次都变成都给在下的妹妹买礼物了。其所图不小,盖一夫一妻。

妹很喜欢线条小狗,正巧最近大悦城也有线条小狗的主题活动,应该有挺多好玩的可以给她买。
灵能百分百

晚饭

大悦城顶层有一家magnet餐厅,西餐,价格适中,氛围感觉也挺不错的,可以买套餐但是好像不接受预定

之后打电话过去问了解到是全时段有空座位的(两位),随时可以去。

冰激凌

好像会分为一般的冰激凌店以及意大利冰激凌(gelato)。之前圣诞节吃的麻布屋就属于gelato,故猜测可能gelato更合xyjj口味,而且糖分添加相较于一般冰激凌也非常少(也就是说,来个巧克力球,也是可以被原谅的吧)。

Gelato

LACOLA

位于静安嘉里中心,距离大悦城有点远但是也串在二号线上,可以先2号线静安寺站下,然后在嘉里中心吃冰激凌,逛一会,再去乘地铁去大悦城。

Dal Cuore(达可芮)

好是好,但是距离地铁站都好远啊

Geato Bello x Fotografiska

大悦城外600米

冰淇淋

MStand

大悦城内,看着好脆的皮

Prefab

Prefab

Prefabs in Unity

A prefab is a template for a game object that can be reused across multiple scenes. Using prefabs can help build scenes faster because a prefab can be created and then reused across multiple scenes without having to recreate the same game object each time.

Creating Prefabs

To create a prefab, first create a game object in the Hierarchy window and add components and child game objects to it until it looks like the desired prefab. Then drag the game object from the Hierarchy window to the Assets folder in the Project window. This will turn the game object into a prefab that can be reused across multiple scenes.

An empty prefab can also be created by right-clicking in the Project window and selecting Create > Prefab, and then editing it in the Inspector window.

Instantiating Prefabs in Scripts

To instantiate an existing prefab in a script, first create a public variable in the script to reference the prefab, and then drag the prefab onto that variable in the Inspector window. For example:

1
2
3
4
5
6
public GameObject myPrefab;

void Start()
{
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
}

The above code creates a public variable named myPrefab and uses the Instantiate method in the Start method to create an instance of the prefab. The prefab can be dragged onto the myPrefab variable in the Inspector window so that it is instantiated at runtime.

To instantiate a prefab from the Assets folder in a script, use the Resources.Load method to load the prefab and then use the Instantiate method to instantiate it. For example:

1
2
3
4
5
void Start()
{
GameObject myPrefab = Resources.Load<GameObject>("MyPrefab");
Instantiate(myPrefab, new Vector3(0, 0, 0), Quaternion.identity);
}

The above code uses the Resources.Load method to load a prefab named MyPrefab from the Resources folder in the Assets folder and then uses the Instantiate method to create an instance of the prefab.

Note: To use the Resources.Load method, the prefab must be located in a Resources folder within the Assets folder.

RoadArchitect

RoadArchitect

Trouble shooting

  1. Pink material
    • Install Universal RP in package manager
    • Edit/Rendering/Materials/Convert…
    • or what about try this fork
UnityRef

WM

Related software

  • Awesome wm
  • compfy (picom with animation, no longer maintained)
  • polybar with polybar-theme (need many requirements i.e. rofi)
  • wezterm

Awesome

github
config
config directory:~/.config/awesome/
doc

youtube

Execute commands on spawn

in the config file rc.lua

1
2
3
awful.spawn("picom --experimental-backend")
awful.spawn("polybar")
awful.spawn.with_shell("xxx.sh")