An Uber driver attacked police with a samurai sword outside Buckingham Palace out of hatred for the Queen, a court has heard.

Mohiussunnath Chowdhury, 27, shouted “Allahu Akbar” – “God is greatest” – as two officers grappled with him near the Queen’s London home in August last year and after he was arrested a suicide note to his sister was found, the Old Bailey heard.

He allegedly wrote: “Tell everyone that I love them and that they should struggle against the enemies of Allah with their lives and their property. The Queen and her soldiers will all be in the hellfire. They go to war with Muslims around the world and kill them without any mercy. They are the enemies that Allah tells us to fight.”

The defendant, of Luton, Bedfordshire, denies preparing acts of terrorism on 25 August last year, claiming he only wanted to get killed.

Opening his Old Bailey trial, Tim Cray, prosecuting, said: “Shortly before 8.30pm on Friday 25 August, this defendant was in his motorcar just outside Buckingham Palace in central London, the court heard. Coming the other direction was a marked police van coming from Charing Cross station.

“As the defendant got up to where the police van was coming towards him, he swerved his car through the traffic cones designed to keep the two lanes of traffic apart.”

Two officers got out to investigate, initially thinking the driver was drunk or on drugs, the court heard.

Cray said: “In fact, as the events in the next few minutes and the detailed investigation later were to prove, we say, this defendant had something far more serious in mind. It was down to the quick reactions of the police the defendant was stopped.

“As officers got out of the car, they heard the defendant say words to the effect: ‘It’s all a bit fucked up.’ They then saw him reach for something that turned out to be a sword. There was a short, desperate struggle with the officers trying to get the sword off the defendant while he is punching at them and they are punching at him.

“The defendant is shouting ‘Allahu Akbar’ over and over again so by now the police officers understandably believed they were dealing with a terrorist incident.”

PC Ian Midgley and DS Gavin Hutt suffered cuts as they disarmed the man, jurors were told.

Cray alleged that at 5.15pm that day the defendant had sent a suicide note to his sister “expressing hate of the Queen and her soldiers” – including the police. He told jurors: “He was saying to his sister that he intended to get to paradise by becoming a martyr fighting ‘the enemies of Allah’.”

Half an hour before he sent the note, he bought a knife sharpener at Sainsbury’s, the court heard. Cray produced a metre-long sword and held it up to show jurors.

He said the incident came six months after the terrorist attacks at Westminster Bridge, and three months after the London Bridge attacks.

Chowdhury was born in London to a “close and supportive family”, the court heard. He had searched the internet for Isis beheadings and Jihadi John, jurors were told. He allegedly discussed the Westminster attacker, Khalid Masood, on WhatsApp, saying: “Fuck the police.”

On the day of the alleged attack, he changed his profile picture to a green bird, in reference to becoming a martyr, jurors heard.