NASA said the revived telescope will be better than ever thanks to the astronauts' efforts and should provide even more dazzling views of the universe for another five to 10 years.
As the spacewalk was drawing to a close, Hubble's chief mechanic John Grunsfeld accidentally bumped one of the telescope's antennas and knocked off its cap with his backpack.
"Oh, I feel terrible," he groaned.
Mission Control quickly assured the astronauts the antenna was fine.
"Sorry Mr. Hubble, have a good voyage," Grunsfeld said after he covered up the tip.
"Consider it a goodbye kiss John," one of his crewmates said. During this last visit to Hubble, the shuttle Atlantis astronauts outfitted the 19-year-old observatory with two state-of-the-art science instruments, and all new batteries and gyroscopes. The $220 million worth of new instruments should allow the telescope to peer even deeper into the cosmos, as far back as 13 billion years.
"This is a really tremendous adventure that we've been on, a very challenging mission," Grunsfeld said. "I want to wish Hubble its own set of adventures and, with the new instruments we've installed, that it may unlock further mysteries of the universe."
Mission Control congratulated the astronauts for the "electronic brain surgery."
It was the fifth and final spacewalk, lasting more than seven hours, for the Atlantis crew. It was also the fifth and final visit by astronauts, ever, to Hubble.
"This is a real great day," Mission Control told the astronauts, "a great way to finish this out."
Keen on leaving the observatory in the best possible shape, Grunsfeld and Andrew Feustel gave the telescope another fresh set of batteries Monday and a new sensor for fine pointing. That left enough time to install steel foil sheets to protect against radiation and the extreme temperature changes of space.
It was messy work. Pieces of the old insulation broke off and floated harmlessly away.
"I was hoping to retrieve those for memories," said Grunsfeld, an astrophysicist who has spent more time working on the orbiting Hubble than anyone. He's visited Hubble twice before, and plans to use the telescope once he's back on Earth to study the moon.
As he applied the new insulation with a roller, a voice from space sang "rollin', rollin', rollin"' to the theme song from the TV show Rawhide.
But the spacewalks were by no means routine. The astronauts had trouble, but did remove an old camera, and had to put in a refurbished pair of gyroscopes after a brand new set refused to go in. And Sunday's spacewalk was particularly exasperating: a stuck bolt almost prevented another team of astronauts from fixing a burned-out science instrument. Brute force saved the day, but so much time was lost that the protective sheets had to be installed Monday.
The shuttle astronauts will set Hubble free Tuesday.
During the mission, the four spacewalkers, two per team, managed to fix two science instruments that had broken down years ago and were never meant to be tinkered with in orbit, and replaced a faltering science data-handling device. They also installed a docking device so a robotic craft can latch on and steer the telescope into the Pacific sometime in the early 2020s.
All told, this visit to Hubble cost more than $1 billion.
NASA hopes to crank Hubble back up by summer's end, following extensive testing of its new parts.
Already, though, scientists have gotten more than they could have hoped out of Hubble, which was launched in 1990 with a projected working lifetime of 15 years. Once its blurred vision was corrected in 1993 and NASA's reputation was restored, the telescope began churning out breathtaking images: among other things, stars in the throes of birth and death.
Back at the launch site, meanwhile, NASA maintained its vigil in case another shuttle needed to rush to the rescue. Atlantis escaped serious launch damage a week ago, but was susceptible to all the space junk in Hubble's 350-mile-high orbit. The astronauts will perform one last survey of their ship after releasing the telescope.
NASA took unprecedented steps to have Endeavour on the pad as a rescue ship, because the Atlantis astronauts have nowhere to seek shelter if they cannot return to Earth because of shuttle damage. The space station is in another, unreachable orbit.
The increased risk prompted NASA to cancel the mission five years ago in the wake of the Columbia accident. It was reinstated two years later.
With NASA's three remaining space shuttles set for retirement next year, there will no way for astronauts to return to Hubble. The new spacecraft under development will be much smaller and less of a workhorse than the shuttle, and lack a big robot arm for grabbing the telescope. Hubble's replacement, the James Webb Space Telescope, will be launched in 2014 by an unmanned rocket and placed in an orbit inaccessible to astronauts.